开发者

Sql Query in Linq

i need to write sql query in string and pass i开发者_如何学Ct to linq like executenonquery() in ado.net but i need to return data from 4 tables Example Query :

SELECT * 
FROM dbo.GeneralAd INNER JOIN
dbo.Category ON dbo.GeneralAd.FkCategoryID = dbo.Category.CategoryID INNER JOIN
dbo.District ON dbo.GeneralAd.FkDistrictID = dbo.District.DistrictID LEFT OUTER JOIN
dbo.Users ON dbo.GeneralAd.FkApprovedByID = dbo.Users.UserID AND dbo.GeneralAd.FKAddedByID = dbo.Users.UserID where .........

how to return data from 4 tables with executenonquery method in linq ?


I'm not sure why you why you want to return all columns from all tables. That seems unnecessary. You can get to the District and Category fields from the GeneralAd object.

What I would probably do is define a property on the General Ad object that executes a select to get the associated User.

If you did want to do it in Linq, you could use something like this:

var results = (from ga in DataContext.GeneralAds
               select new 
               { 
                   Col1 = ga.Col1,
                   Col2 = ga.Category.Col2,
                   Col3 = ga.District.Col3,
                   Col4 = (from u in DataContext.Users
                           where u.UserID = ga.FkApprovedByID AND u.UserID = ga.FKAddedByID
                           select u.Col4)
               });

You might have to add an Any() method on Col4:

var results = (from ga in DataContext.GeneralAds
               select new 
               { 
                   Col1 = ga.Col1,
                   Col2 = ga.Category.Col2,
                   Col3 = ga.District.Col3,
                   Col4 = ((from u in DataContext.Users
                            where u.UserID = ga.FkApprovedByID AND u.UserID = ga.FKAddedByID
                            select u.Col4).Any() ? 
                              (from u in DataContext.Users
                               where u.UserID = ga.FkApprovedByID AND u.UserID = ga.FKAddedByID
                               select u.Col4).First() : -1)
               });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜