在linq连接中使用相等且不相等 [英] using equal and not equal in a linq join

查看:159
本文介绍了在linq连接中使用相等且不相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 选择Or.Amount,Usr.Name,Usr.Email 
从[order] as or
left join vw_AllUsers as Usr on Usr.UserId = Or.UserId and Usr.RoleName<> 'Admin'

我找不到使用相等的方法不等于
如果 Usr.RoleName管理员 Usr.RoleName ='Admin',linq语句可以写成这样

  var result = from or in context.orders 
join Usr in context.vw_AllUsers on
new {userid = Or.UserId,role =Admin}
等于
new {userid = Usr.UserId,role = Usr.RoleName}
select ........

或者我可以处理它在linq的一部分获得相同的结果,如下

 其中!Usr.RoleName.Equals(Admin)

但是可以在linq的加入部分处理这个问题吗?



谢谢提前

解决方案

LINQ只支持平等joi ns,没有办法在 join 本身中使用不同的运算符。



正如您所指出的,您可以使用其中语句具有相同的效果。如果您没有参加平等比较,您可以使用子句中的多个



从MSDN


等于 operator



A join 执行等等。换句话说,您只能在两个键的相等性上进行匹配。不支持其他类型的比较,例如大于或不等于。要清楚所有连接都是等价, join 子句使用等于关键字而不是==运算符。 等于关键字只能在 join 子句中使用,它与==操作符的区别在于一个重要的方式。使用等于,左键消耗外部源序列,右键消耗内部源。外部源仅在等于的左侧的范围内,内部源序列仅在右侧的范围内。



非Equijoins



您可以通过使用多个从 执行非等价,交叉连接和其他自定义联接操作, code>子句将新的序列独立地引入到查询中。有关详细信息,请参阅如何执行自定义联接操作(C#编程指南)



I am trying to convert following SQL query to linq;

select Or.Amount, Usr.Name, Usr.Email
from [order] as Or
left join vw_AllUsers as Usr on Usr.UserId = Or.UserId and Usr.RoleName <> 'Admin'

I couldn't find a way to use equal and not equal in the same join.. If Usr.RoleName <> 'Admin' was Usr.RoleName = 'Admin', the linq statement could be written like that

var result =  from Or in context.orders
              join Usr in context.vw_AllUsers on 
              new { userid = Or.UserId, role = "Admin"}
              equals
              new { userid = Usr.UserId, role = Usr.RoleName}
              select ........

or i can handle it in where part of the linq to get same result, as follows

where !Usr.RoleName.Equals("Admin")

but is it possible to handle this in join part of the linq ?

Thanks in advance

解决方案

LINQ only supports equality joins, there's not a way to use a different operator in the join itself.

As you point out, you can just use a where statement for the same effect. If you didn't have an equality comparison to join on, you can use multiple from clauses.

From MSDN:

The equals operator

A join clause performs an equijoin. In other words, you can only base matches on the equality of two keys. Other types of comparisons such as "greater than" or "not equals" are not supported. To make clear that all joins are equijoins, the join clause uses the equals keyword instead of the == operator. The equals keyword can only be used in a join clause and it differs from the == operator in one important way. With equals, the left key consumes the outer source sequence, and the right key consumes the inner source. The outer source is only in scope on the left side of equals and the inner source sequence is only in scope on the right side.

Non-Equijoins

You can perform non-equijoins, cross joins, and other custom join operations by using multiple from clauses to introduce new sequences independently into a query. For more information, see How to: Perform Custom Join Operations (C# Programming Guide).

这篇关于在linq连接中使用相等且不相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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