Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

c# - Adding LINQ to my classes

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).

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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;

the C# compiler translates this into:

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

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;
      }
    }
  }
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...