实体框架左连接 [英] Entity framework left join

查看:125
本文介绍了实体框架左连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以返回所有u.usergroups如何改变这个查询?

 从u在用户组
从u.UsergroupPrices p
选择新UsergroupPricesList
{
UsergroupID = u.UsergroupID,
UsergroupName = u.UsergroupName,
价格= p.Price
};


解决方案

改编自MSDN,如何离开加入EF使用4

  VAR的查询=从u在用户组
            加入UsergroupPrices P于ü等于p.UsergroupID到GJ
            从X在gj.DefaultIfEmpty()
            选择新{
                UsergroupID = u.UsergroupID,
                UsergroupName = u.UsergroupName,
                价格=(○×== NULL的String.Empty:x.Price)
            };

How do I change this query so it returns all u.usergroups?

from u in usergroups
from p in u.UsergroupPrices
select new UsergroupPricesList
{
UsergroupID = u.UsergroupID,
UsergroupName = u.UsergroupName,
Price = p.Price
};

解决方案

adapted from MSDN, how to left join using EF 4

var query = from u in usergroups
            join p in UsergroupPrices on u equals p.UsergroupID into gj
            from x in gj.DefaultIfEmpty()
            select new { 
                UsergroupID = u.UsergroupID,
                UsergroupName = u.UsergroupName,
                Price = (x == null ? String.Empty : x.Price) 
            };

这篇关于实体框架左连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆