EF 4.0 Linq to Enity 查询

发布于 2024-11-07 12:09:21 字数 185 浏览 0 评论 0原文

大家好,我需要在 Linq to Entity 中复制此 SQL 查询,

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

  where b.Username = 'Name'

可能有人会有所帮助。

Hi guys i need 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捂风挽笑 2024-11-14 12:09:21

试试这个:

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

假设 Subscriber 具有引用用户实例的导航属性 User

如果您想使用联接(不需要),您可以将其用于内联接:

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
                };

或将其用于左外联接:

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,
                };

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,
                };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文