帮助了解Enumerable.Join方法 [英] Help Understanding Enumerable.Join Method
问题描述
昨天,我发布了此问题,内容涉及在Join()方法中使用lambda来检查两个实体之间是否存在两个条件.我收到了关于该问题的答案,效果很好.我想在阅读了有关Enumerable.Join()方法的MSDN文章后,我会确切地了解正在发生的事情,但我没有.有人可以帮助我了解下面的代码(特别是Join()方法)中发生了什么吗?预先感谢.
Yesterday I posted this question regarding using lambdas inside of a Join() method to check if 2 conditions exist across 2 entities. I received an answer on the question, which worked perfectly. I thought after reading the MSDN article on the Enumerable.Join() method, I'd understand exactly what was happening, but I don't. Could someone help me understand what's going on in the below code (the Join() method specifically)? Thanks in advance.
if (db.TableA.Where( a => a.UserID == currentUser )
.Join( db.TableB.Where( b => b.MyField == someValue ),
o => o.someFieldID,
i => i.someFieldID,
(o,i) => o )
.Any())
{
//...
}
具体来说,我对最后三个参数以及实际情况感到好奇.它们如何导致Func(TOuter,TKey),Func(TInner,TKey)等的签名要求.
Specifically, I'm curious about the last 3 parameters, and what's actually going on. How do they result in the signature requirements of Func(TOuter, TKey), Func(TInner, TKey) etc.
推荐答案
Eric和Nick都提供了很好的答案.
Eric and Nick have both provided good answers.
您还可以使用查询语法(相对于示例中使用的方法语法)编写Linq查询表达式:
You may also write the Linq query expression using query syntax (vs. method syntax, which you are using in your example):
var query = from a in db.TableA
join b in db.TableB on a.someFieldID equals b.someFieldID
where a.UserID == currentUser && b.MyField == someValue
select a;
if (query.Any()) {
...
}
更新:
您似乎被lambda表达式所困扰.这是一个像变量一样传递的函数. Lambda表达式等效于匿名委托(或更匿名的匿名方法).
You seem to be stuck on the lambda expressions. It's a function that you pass around like a variable. A lambda expression is equivalent to an anonymous delegate (or anonymous method, to me more general).
这是您使用lambda表达式作为委托的查询(当然,将EntityType替换为从TableA返回的实体类型):
Here is your query with the lambda expressions as delegates (replace EntityType with the type of your entity returned from TableA, of course):
if (db.TableA.Where( delegate(EntityType a) { return a.UserID == currentUser; } )
.Join( db.TableB.Where( delegate(EntityType b) { return b.MyField == someValue; } ),
delegate(EntityType o) { return o.somefieldId); },
delegate(EntityType i) { return i.someFieldId); },
delegate(EntityType o, EntityType i) { return o; })
.Any())
{ //... }
{ //... }
注意:lambda表达式具有许多重要方面,这不仅使它等同于匿名方法.我建议您浏览其他SO问题,尤其是在线阅读有关lambda表达式的信息.它们允许以更简单,更优雅的方式表达非常有力的想法.这是一个很深的话题,但基础知识很容易理解.您可以像传递变量一样传递该函数,也可以将其作为参数传递给其他函数.
NOTE: A lambda expression has important aspects that make it more than just an equivalent for anonymous methods. I recommend that you look through other SO questions and read online about lambda expressions in particular. They allow for very powerful ideas to be expressed in a much simpler and elegant way. It's a deep topic, but the basics are easy enough to understand. It's a function that you can pass around like a variable, or as a parameter to other functions.
这篇关于帮助了解Enumerable.Join方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!