linqlinq from join onn 后多个条件怎么写

linq里lambda写的join查询,并附加动态拼接的条件,条件为enum类型的查询 - Arik lee - 推酷
linq里lambda写的join查询,并附加动态拼接的条件,条件为enum类型的查询 - Arik lee
因为查询条件不固定的原因,sql式的linq查询没法动态拼接条件。
网上搜的资料整理之后终于解决。
参考资料:
enum使用 http://blog.csdn.net/slowlifes/article/details/7799444
linq动态查询 /zh-cn/subscriptions/dd470085.aspx
lambda join使用 http://blog.csdn.net/lai_gb/article/details/4491843
       string whereParams = GetWhereParams(SolutionGroup, CapabilityType);
var query = crm.new_usercapabilitynew_usercapabilities.Where(uc =& uc.new_user == SystemUserId).
crm.new_capabilitynew_capabilities,
uc =& uc.new_capabilityid,
c =& c.new_capabilityid,
(uc, c) =& new
).Where(o =& o.c.new_name.Contains(Capability)).Where(whereParams)
.OrderBy(o =& o.uc.new_capabilityidLabel)
.Select(o =& new
new_capabilityid = o.uc.new_capabilityid,
new_usercapabilityid = o.uc.new_usercapabilityid,
new_capabilityidLabel = o.uc.new_capabilityidLabel,
new_developmentarea = o.uc.new_developmentarea,
new_rating = o.uc.new_rating
     public string GetWhereParams(string SolutionGroup, string CapabilityType)
var enumgroup = Enum.GetNames(typeof(Xrm.new_capability.NewSolutiongroup)).Where(e =& e.ToLower().Contains(SolutionGroup.ToLower())).ToList();
string groupLinqStr = &&;
if (enumgroup.Count & 0)
groupLinqStr += &(&;
for (int i = 0; i & enumgroup.C i++)
Xrm.new_capability.NewSolutiongroup enumitem = (Xrm.new_capability.NewSolutiongroup)Enum.Parse(typeof(Xrm.new_capability.NewSolutiongroup), enumgroup[i]);
groupLinqStr += &c.new_solutiongroup = & + ((int)enumitem).ToString();
if (i != enumgroup.Count - 1)
groupLinqStr += & or &;
groupLinqStr += &)&;
else if (!string.IsNullOrEmpty(SolutionGroup))
groupLinqStr += &c.new_solutiongroup = 0&;
var enumtype = Enum.GetNames(typeof(Xrm.new_capability.NewCapabilitytype)).Where(e =& e.ToLower().Contains(CapabilityType.ToLower())).ToList();
if (enumtype.Count & 0)
if (groupLinqStr.Length & 0) groupLinqStr += & and &;
groupLinqStr += &(&;
for (int i = 0; i & enumtype.C i++)
Xrm.new_capability.NewCapabilitytype enumitem = (Xrm.new_capability.NewCapabilitytype)Enum.Parse(typeof(Xrm.new_capability.NewCapabilitytype), enumtype[i]);
groupLinqStr += &c.new_capabilitytype = & + ((int)enumitem).ToString();
if (i != enumtype.Count - 1)
groupLinqStr += & or &;
groupLinqStr += &)&;
else if (!string.IsNullOrEmpty(CapabilityType))
if (groupLinqStr.Length & 0) groupLinqStr += & and &;
groupLinqStr += &c.new_capabilitytype = 0&;
return groupLinqS
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致15137人阅读
文章转自:
我们在做SQL查询的时候经常会用到Inner Join,Left Join,笛卡尔积等等,连接方式的概念方面我想也不用给予太多解释,
我们今天的重点是让大家熟悉LINQ是如何使用Join来实现常用的表连接的。
创建测试用类:
class Customer
public int CustomerId { }
public string Name { }
public int Age { }
class Product
public int ProductId { }
public string Name { }
public string Origin { }
class Order
public int OrderId { }
public int CustomerId { }
public List&Product& Products { }
我们用以下例子来熟悉 Join 关键字的用法。
1.Inner Join:
CreateEntities();
var query = from c in customers
join o in orders on c.CustomerId equals o.CustomerId
where o.OrderId == 2
foreach (var customer in query)
Console.WriteLine(&Id:{0}, Name:{1}&, customer.CustomerId, customer.Name);
运行结果:
  Id:1, Name:CA   
 上面这个是常见的内连接的例子,和SQL语法也很相似,但有以下几点要注意:
(1).连接条件: c.CustomerId equals o.CustomerId 只能使用 equals 不能用 =,==,等于 等表示。
以为LINQ的设计者认为 几乎所有的连接条件都是 = 条件不会出现 &,&,!= 等情况因此使用了个关键字来描述表连接条件。
(2).条件顺序:c.CustomerId equals o.CustomerId ,range variable: c 和b之前的顺序不能颠倒。
2.Group Join:
也许大家对Group Join的概念不太了解,没关系让我们通过例子来认识它:
CreateEntities();
var query = from c in customers
join o in orders on c.CustomerId equals o.CustomerId into os
select new { c, os };
foreach (var item in query)
Console.WriteLine(&Customer Id:{0}, Name:{1}&, item.c.CustomerId, item.c.Name);
foreach (var o in item.os)
Console.WriteLine(&--Order Id:{0}&, o.OrderId);
Customer Id:1, Name:CA
--Order Id:1
--Order Id:2
Customer Id:2, Name:CB
--Order Id:4
Customer Id:3, Name:CC
--Order Id:3
Customer Id:4, Name:CD
Press any key to continue . . .
以上查询返回的结果:和Group By 返回的结果非常的相似:一个KEY对象对应一个集合。
要实现Group Join我们要引入一个关键字:into
但使用时要注意一下几点:
(1).使用into 关键字后 join 后面的 range variable:o 在后面的表达式块中就失去了作用域。
(2).range variable:os 通常情况下都是IEnumerable&T&类型的。
3.Left Join:
 Left Join 我们在SQL里经常用到,让我们来看看LINQ里怎么实现它:
CreateEntities();
var query = from c in customers
join o in orders on c.CustomerId equals o.CustomerId into os
from o2 in os.DefaultIfEmpty(
new Order { OrderId = 0, CustomerId = 0, Products = new List&Product&() })
select new { c, o2 };
foreach (var item in query)
Console.WriteLine(&Customer Id:{0}, Name:{1}--Order Id:{0}&,
item.c.CustomerId, item.o2.OrderId);
Customer Id:1, Name:1--Order Id:1
Customer Id:1, Name:2--Order Id:1
Customer Id:2, Name:4--Order Id:2
Customer Id:3, Name:3--Order Id:3
Customer Id:4, Name:0--Order Id:4
Press any key to continue . . .&
我们可以看到Left Outer Join 的语法进一步的复杂化了,结果也有细微的不同。
(1).从语法上:
from o2 in os.DefaultIfEmpty(
&&&&&&&&&&&&&&& new Order { OrderId = 0, CustomerId = 0, Products = new List&Product&() })
主要区别在于以上者1句语句。查询方法DefaultIfEmpty 用于定义当查询记录为空时,预定义默认值。再从其集合中取出子元素。
(2).从结果上: 我们在遍历查询结果时可以发现Left Join相似于Inner Join结果都是“平面”的,然而Group Join返回的结果具有层次性。
由于C#是面向对象的,往往会通过对象与对象间的外系来实现数据间关系。有时表达2个之间的关系也可以不使用Join关键字,
因此Join关键字其实在实际LINQ查询表达式中用的不是很多。
站内连接:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1919683次
积分:10755
积分:10755
排名:第1278名
原创:74篇
转载:66篇
评论:281条
(2)(1)(1)(7)(2)(1)(1)(1)(1)(2)(1)(1)(1)(6)(3)(1)(3)(3)(2)(5)(2)(1)(5)(4)(2)(3)(3)(1)(2)(6)(13)(20)(14)(14)(9)var a = from m in DbContext.Set&T1&()
join q in DbContext.Set&T2&()
new { m.ID, Phone=m.Phone1 } equals new { q.ID, Phone=q.Phone2 }
where m.Phone1 !=null
select new { m.ID, m.Phone1 };
a = a.OrderBy(m =& m.Phone1).Skip(2).Take(2);
[Extent1].[ID] AS [ID],
[Extent1].[Phone1] AS [Phone1],
[dbo].[T1] AS [Extent1]
INNER JOIN [dbo].[T2] AS [Extent2] ON ([Extent1].[ID] = [Extent2].[ID]) AND (([Extent1].[Phone1] = [Extent2].[Phone2]) OR (([Extent1].[Phone1] IS NULL) AND ([Extent2].[Phone2] IS NULL)))
WHERE [Extent1].[Phone1] IS NOT NULL
所以linq为什么要这么写,看到生成的sql语句 就不言而喻了,因为linq多管闲事的将NULL给总结进去了
阅读(...) 评论()

我要回帖

更多关于 linq join on 多条件 的文章

 

随机推荐