复杂的LINQ查询

时间:2016-04-26 14:34:34

标签: c# asp.net linq

These are my two tables. How can i make LINQ query between two tables :

(1) Users : 
       Id , Name, Email
(2) Articles : 
       Id,Title, Description ,UserId (Foreign key reference to User Table Id column)

Questions :

(1)  How to select all records that are similar in both tables ? (Inner Join)
(2)  Left Join (Only record of left table)
(3)  Right Join (Only record of right table)
(4)  Full outer join (All records of both tables)

请帮我创建带有连接的LINQ查询,因为我是LINQ to SQL的初学者。

1 个答案:

答案 0 :(得分:2)

以下是所有伪代码,您需要根据您的桌面名称和需求进行调整

 var question_one = (from u in Users
                        join a in Articles on u.Id equals a.UserId)

var question_two = (from u in user
                    join a in Articles on u.Id equals a.UserId into leftJoin
                    from subUser in leftJoin.DefaultIfEmpty()
                    select subUser)

var question_three = (from a in Articles
                    join  u in user  on a.UserId equals u.Id  into leftJoin
                    from subUser in leftJoin.DefaultIfEmpty()
                    select subUser)

问题四无法在传统的外连接中完成,因为,articles表实际上是用户的子表。因此,任何文章都保证始终拥有用户。你可以做的就是用你的用户表进行电子化,并包括任何类似的文章

警告伪代码,因为我不知道您的导航属性

Users.include(x => x.Articles).ToList();

msdn for linq joining