How to retrieve all columns from table1 and matching columns from table2(Left outer join) using Linq

偶尔善良 提交于 2019-12-12 10:22:19

问题


I have to retrieve all the columns from table1 and matching columns from table2. I have a stored procedure as :

alter  Procedure [dbo].[usp_Property]
@UserId bigint =null
As
Begin
select P.PID, P.PropertyName, P.SBUArea, P.ListedOn, 
       P.Availability, P.Price,   F.UserID, F.PID as FavProjId  
       from dbo.Property P left outer join dbo.Favorite F
       on (F.PID=P.PID And F.UserID=@UserId)

I want to get Linq query for the same. So far I tried with something like

//User Id comes from session..
//var userId
var result=(from p in Properties
                   join f in Favorites
                   on p.PID equals f.PID into r
                   from r1 in r.DefaultIfEmpty()
                   where r1.UserID==userId
                   select new
                   {
                     p.PID,
                      p.PropertyName,                       
                      p.SBUArea, p.ListedOn,
                      r1.UserId
                   });

Can anyone please correct me. I want to use left outer join or any other alternate thing here.


回答1:


If I beautify your SP's code, I get this:

DECLARE @UserId int 
SET @UserId = 12435 

SELECT 
     P.PID
    ,P.PropertyName
    ,P.SBUArea
    ,P.ListedOn 
    ,P.Availability
    ,P.Price
    ,F.UserID
    ,F.PID AS FavProjId  
FROM Property AS P 
LEFT JOIN Favorite AS F
    ON (F.PID=P.PID AND F.UserID = @UserId)

Now I wonder if you need that UserId in the WHERE clause of the SQL, or really in the join.

But anyway, here the LINQ-equivalent of exactly that SQL:

System.Int64 __UserId = 12435;

var query = (
    from P in Repo.Property
    from F in Repo.Favorite
         .Where(fav=> fav.PID == P.PID && fav.UserID == __UserId)
         .DefaultIfEmpty() // <== makes join left join
    select new
    {
         PID = P.PID
        ,PropertyName = P.PropertyName
        ,SBUArea = P.SBUArea
        ,ListenOn = P.ListedOn 
        ,Availabiity = P.Availability
        ,Price = P.Price
        ,UserId = F.UserID
        ,FavProjId = F.PID 
    }

);

var data = (query).ToList();



回答2:


Use anonymous objects in your selection

var result = from t in table1
             join x in table2
             on t.id equals x.id
             select new { id = t.id, col1 = t.col1, col2 = x.col2 }



回答3:


If you will put the where clause after join you may get null reference exception because DefaultIfEmpty returns default value for non matching rows. You can filter the records before joining itself like this:-

var result=(from p in Properties
            join f in Favorites.Where(x => x.UserID == userId)
            on p.PID equals f.PID into r
            from r1 in r.DefaultIfEmpty()
            select new
            {
                p.PID,
                p.PropertyName,                       
                p.SBUArea, 
                p.ListedOn,
                r1.UserId
            });

Please note you need to access properties of Favorites using r1.

Update:

As far as I have understood you need all records from Property table and only matching rows from Favorite table. But you have a filter on your Favorite table so the ultimate data source will differ. Let me make my point clear by this example:-

Suppose you have following data in Property table:-

PID    PropertyName    Availability    Price
 1          aaa            true         20
 2          bbb            false        10
 3          ccc            true         50
 4          ddd            false        80
 5          eee            true         55
 6          fff            false        70

and Favorite table like this:-

FID    PID    UserId
 1      4      1001
 2      2      1005
 3      5      1007

And let's say you want all records for UserId 1005, then the result should contain all the property Id's from 1 till 6 even if UserId 1005 doesn't match for property Id's 4 & 2 right? So the query above is as per this understanding. Check this Fiddle with same example and output.



来源:https://stackoverflow.com/questions/31938784/how-to-retrieve-all-columns-from-table1-and-matching-columns-from-table2left-ou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!