将LINQ添加到我的班级 [英] Adding LINQ to my classes

查看:62
本文介绍了将LINQ添加到我的班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Apple属性列表(属性列表)解析器中添加LINQ支持(二进制,JSON和XML),这样我就可以像使用System.Xml一样在其上使用SQL一样的查询.它通过完全解析文件并将其保存在堆中来工作. (就像System.Xml一样.)

I want to add LINQ support to my Apple Property List (Property List) parser (binary, JSON, and XML) so that I can use SQL like queries on it like you can with System.Xml. It works by parsing the file completely and saving it on the heap. (like System.Xml does).

我一直在谷歌搜索,似乎一点也不了解(我有很多Using LINQ to SQL Classes页). MSDN 上的指南,我也不明白.我该怎么办?

I have been Googling and can't seem to understand any of it (I get a lot of Using LINQ to SQL Classes pages). The guide over on MSDN, I don't understand either. What do I need to do?

推荐答案

实施LINQ只是意味着使用正确的签名来实现LINQ期望存在的方法,例如WhereSelect.尽管有普遍的看法,但您不必为您的类实现IEnumerable接口即可支持LINQ.但是,实现IEnumerable将自动 几乎免费获得Enumerable类上的整个LINQ方法集-您只需实现GetEnumeratorIEnumerator类.

Implementing LINQ simply means implementing the methods LINQ expects to be there, such as Where or Select, with the correct signatures. Despite common perception, you do not have to implement the IEnumerable interface for your class to support LINQ. However, implementing IEnumerable will automatically get you the entire set of LINQ methods on the Enumerable class almost for free -- you only have to implement GetEnumerator and an IEnumerator class.

在我的博客上,有一些关于如何实现IEnumerable的示例,在此帖子中,有关

There are a couple of examples on how to implement IEnumerable on my blog, in this post about the Iterator pattern.

但是,如果您的班级无法枚举是没有道理的,则无需这样做.您只需要直接实现适当的LINQ方法.只要C#编译,LINQ实际上并不关心方法的定义方式.也就是说,如果您写:

However, if it doesn't make sense for your class to be enumerable, you don't need to do so. You just need to implement the appropriate LINQ methods directly. LINQ doesn't actually care how the methods get defined, so long as the C# compiles. That is, if you write:

from p in myPlist where p.Thing == "Thing" select p;

C#编译器将其翻译为:

the C# compiler translates this into:

mpPlist.Where(p => p.Thing == "Thing").Select(p => p);

只要能编译,LINQ就会起作用.要查看这些方法的正确签名,请查看MSDN文档的LINQ查询方法的列表. .例如(假设您的PListPListItem s的列表):

As long as that compiles, LINQ will work. To see the correct signatures for the methods, look as the MSDN documentation's list of LINQ query methods. For example (assume that your PList was a list of PListItems):

public class PList
{
  public IEnumerable<PListItem> Where(Func<PListItem, bool> predicate)
  {
    foreach (var item in this.items)
    {
      if (predicate(item)) 
      {
        yield return item;
      }
    }
  }
}

以这种方式直接实现LINQ可以使您更好地控制其行为,但要使其正确运行还需要进行很多工作,并且需要了解返回值的含义,通常,如果您可以放弃让类实现IEnumerable并让C#为您完成所有工作,那么事情就容易得多了.

While implementing LINQ directly in this manner gives you a lot more control over how it behaves, it's a lot more work to get it right, and you need to understand the implications of your return values, and chaining LINQ calls, etc. In general, if you can get away with making your class implement IEnumerable and let C# do all the work for you, things go much easier.

这篇关于将LINQ添加到我的班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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