开发者

EF 4.0 Linq to Enity query

开发者 https://www.devze.com 2023-03-06 05:37 出处:网络
Hi guys i ne开发者_C百科ed to replicate this SQL query in Linq to Entity select * from Subscriber a

Hi guys i ne开发者_C百科ed to replicate this SQL query in Linq to Entity

  select * from Subscriber a
  inner join User b on a.UserId = b.Id

  where b.Username = 'Name'

May be some one may help.


Try this:

var query = from s in context.Subscribers.Include("User")
            where s.User.Username == "Name"
            select s;

This suppose that Subscriber has navigation property User referencing the user instance.

If you wan to use join (which is not needed) you can use this for inner join:

var query = from s in context.Subscribers
            join u in context.Users on s.User.Id equals u.Id
            where u.Username == "Name"
            select new 
                {
                    Subscriber = s, 
                    User = u
                };

or this for left outer join:

var query = from s in context.Subscribers
            join u in context.Users on s.User.Id equals u.Id into x
            where u.Username == "Name"
            from y in x.DefaultIfEmpty()
            select new 
                {
                    Subscriber = s 
                    User = y,
                };
0

精彩评论

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