我需要在字符串中编写sql查询并将其传递给linq,就像ado.net中的executenonquery()一样 但我需要从4个表中返回数据 示例查询:
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 .........
如何使用linq中的executenonquery方法从4个表中返回数据?
答案 0 :(得分:0)
我不知道你为什么要返回所有表中的所有列。这似乎没必要。您可以从GeneralAd对象进入区域和类别字段。
我可能会做的是在General Ad对象上定义一个属性,该属性执行select以获取关联的User。
如果您确实想在Linq中执行此操作,可以使用以下内容:
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)
});
您可能必须在Col4上添加Any()方法:
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)
});