简体   繁体   English

代表和Lambdas和LINQ,哦,我的!

[英]Delegates and Lambdas and LINQ, Oh My!

As a fairly junior developer, I'm running into a problem that highlights my lack of experience and the holes in my knowledge. 作为一个相当初级的开发人员,我遇到了一个问题,突出了我缺乏经验和我的知识漏洞。 Please excuse me if the preamble here is too long. 如果这里的序言太长,请原谅。

I find myself on a project that involves my needing to learn a number of new (to me) technologies, including LINQ (to OBJECTS and to XML for purposes of this project) among others. 我发现自己正处于一个涉及我需要学习一些新的(对我而来)技术的项目,包括LINQ(对于这个项目来说是OBJECTS和XML)等等。 Everything I've read to this point suggests that to utilize LINQ I'll need to fully understand the following (Delegates, Anonymous Methods and Lambda Expressions). 我在这一点上所读到的一切都表明,要利用LINQ,我需要完全理解以下内容(代理,匿名方法和Lambda表达式)。

OK, so now comes the fun. 好的,所以现在很有趣。 I've CONSUMED delegates in the past as I have worked with the .NET event model, but the majority of the details have been hidden from me (thanks Microsoft!). 我曾经使用过.NET事件模型,但是我已经消费了代表,但是大部分细节都是隐藏的(感谢Microsoft!)。 I understand that on a basic level, delegate instances are pointers to methods (a gross over-simplification, I know). 我理解在基本层面上,委托实例是指向方法的指针(我知道这是一个过度简化的过程)。

I understand that an anonymous method is essentially an in-line unnamed method generally (if not exclusively) created as a target for a delegate. 据我所知,匿名方法本质上是一种内联的未命名方法,通常(如果不是唯一的话)创建为委托的目标。

I also understand that lambdas are used in varying ways to simplfy syntax and can be used to point a simple anonymous method to a delegate. 我也理解lambdas以不同的方式用于简化语法,可用于将简单的匿名方法指向委托。

Pardon me if my any of my descriptions are WAY off here, this is the basic level to which I understand these topics. 请原谅我,如果我的任何描述都在这里,那么这就是我理解这些主题的基本层面。

So, the challenge: 所以,挑战:

  1. Can anyone tell me if at least on a basic level if my understanding of these items is even close? 如果我对这些项目的了解甚至更接近,至少在基本水平上有人能告诉我吗? I'm not looking for complex esoteric minutiae, just the basics (for now). 我不是在寻找复杂的深奥细节,只是基础(现在)。

  2. To what degree do I need to truly understand these concepts before applying LINQ in a project to reasonable effect? 在项目中应用LINQ以达到合理的效果之前,我需要在多大程度上真正理解这些概念? I want to understand it fully and am willing to spend the time. 我想完全理解它,并愿意花时间。 I simply may not HAVE the time to fully grok all of this stuff before I need to produce some work. 在我需要完成一些工作之前,我可能没有时间完全理解所有这些东西。

  3. Can anyone point me to some good articles that explain these subjects and apply them to "real world" examples so that I can get my head around the basics of the topics and application of them? 任何人都可以向我指出一些解释这些主题的好文章,并将它们应用到“真实世界”的例子中,这样我就可以了解这些主题和应用的基础知识吗? What I mean by real world, is how might I use this in the context of "Customers and Invoices" rather than abstract "Vectors and Shapes" or "Animals and Cows". 我的意思是现实世界,我是如何在“客户和发票”的背景下使用它而不是抽象的“矢量和形状”或“动物和奶牛”。 The scenario can be somewhat contrived for demonstration purposes, but hopefully not strictly academic. 出于演示目的,这种情况可能有点人为,但希望不是严格的学术性的。 I have found a number of examples on-line and in books, but few seem to be "Plain English" explanations. 我在网上和书中找到了一些例子,但似乎很少有“简单英语”的解释。

Thank you all in advance for your patience, time and expertise. 提前感谢您的耐心,时间和专业知识。

Where can i find a good in depth guide to C# 3? 我在哪里可以找到C#3的深度指南?

1) Your knowledge so far seems ok. 1)到目前为止你的知识似乎没问题。 Lambda expressions are turned into anonymous methods or System.Linq.Expressions.Expression's, depending on context. Lambda表达式将转换为匿名方法或System.Linq.Expressions.Expression,具体取决于上下文。 Since you aren't using a database technology, you don't need to understand expressions (all lambdas will be anonymous methods). 由于您没有使用数据库技术,因此您不需要理解表达式(所有lambdas都是匿名方法)。 You didn't list Extension methods, but those are very important (and easy) to understand. 您没有列出扩展方法,但这些方法非常重要(并且易于理解)。 Make sure you see how to apply an extension method to an interface - as all the functionality in linq comes from System.Linq.Enumerable - a collection of extention methods against IEnumerable(Of T). 确保你看到如何将扩展方法应用于接口 - 因为linq中的所有功能都来自System.Linq.Enumerable - 一组针对IEnumerable(Of T)的扩展方法。

2) You don't need a deep understanding of lambdas. 2)你不需要对lambdas有深刻的理解。

The arrow syntax ( => ) was the biggest hurdle for me. 箭头语法(=>)对我来说是最大的障碍。 The arrow separates the signature and the body of the lambda expression. 箭头分隔lambda表达式的签名和正文。

Always remember : Linq methods are not executed until enumerated. 永远记住: 在枚举之前不会执行Linq方法。

Watch out for using loop variables in a lambda . 注意在lambda中使用循环变量 This is a side effect from deferred execution that is particularly tricky to track down. 这是延迟执行的副作用,特别难以追踪。

3) Sure, Here are some of my answers that show linq method calls - some with xml. 3)当然,这里有一些我的答案显示linq方法调用 - 一些用xml。

1) Those descriptions sound pretty accurate to me. 1)这些描述对我来说听起来非常准确。 Sometimes anonymous methods and lambda expressions will need to create a new type to put the target of the delegate in, so they can act as closures . 有时,匿名方法和lambda表达式需要创建一个新类型来放置委托的目标,因此它们可以充当闭包

2/3) I would read up a bit until you're happy with delegates, anonymous methods and lambda expressions. 2/3)我会读到一些内容,直到你对委托,匿名方法和lambda表达式感到满意为止。 I dedicate a chapter to the delegate-related changes in each of C# 2.0 and C# 3.0 in C# in Depth, although of course other books go into detail too. 我在C#2.0深度介绍了C#2.0和C#3.0中与代理相关的更改,但当然其他书籍也会详细介绍。 I have an article as well, if that helps. 我也有一篇文章 ,如果有帮助的话。

As for examples - delegates are used for many different purposes. 至于示例 - 委托用于许多不同的目的。 They're all different ways of looking at the same functionality, but they can feel very different: 它们是查看相同功能的不同方式,但它们可以感觉非常不同:

  • Providing the code to call when you start a new thread 提供在启动新线程时调用的代码
  • Reacting to UI events 对UI事件做出反应
  • Providing the filter, selection, ordering etc for a LINQ query 为LINQ查询提供过滤器,选择,排序等
  • Providing a callback for when an asynchronous operation has finished 为异步操作完成时提供回调

If you have any specific situations you'd like an example of, that would be easier to answer. 如果你有任何特定的情况,你想要一个例子,那将更容易回答。

EDIT: I should point out that it's good news that you're only working with LINQ to Objects and LINQ to XML at the moment, as that means you don't need to understand expression trees yet. 编辑:我应该指出,你现在只使用LINQ to Objects和LINQ to XML这是一个好消息,因为这意味着你还不需要理解表达式树。 (They're cool, but one step at a time...) LINQ to XML is really just an XML API which works nicely with LINQ - from what I remember, the only times you'll use delegates with LINQ to XML are when you're actually calling into LINQ to Objects. (它们很酷,但是一步一步......)LINQ to XML实际上只是一个与LINQ很好地结合使用的XML API - 从我记忆中来看,你唯一能够使用LINQ to XML的委托就是你实际上正在调用LINQ to Objects。 (That's very nice to do, admittedly - but it means you can reuse what you've already learned.) (诚​​然,这是非常好的事情 - 但这意味着你可以重复使用你已经学过的东西。)

As you've already got C# in Depth, chapters 10 and 11 provide quite a few examples of using lambda expressions (and query expressions which are translated into lambda expressions) in LINQ. 由于您已经在深度中获得了C#,因此第10章和第11章提供了一些在LINQ中使用 lambda表达式(以及转换为lambda表达式的查询表达式)的示例。 Chapter 5 has a few different examples of delegate use. 第5章有几个不同的委托使用示例。

Read this... 读这个...

http://linqinaction.net/ http://linqinaction.net/

..and all you're question will be answered!!! ..你所有的问题都将得到解答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM