我错过了LINQ的一些内容吗?

我似乎错过了一些关于LINQ的东西。 对我来说,看起来它正在采用我最喜欢的SQL的一些元素,并将它们转移到C#语言中并将它们用于其他事情。

我的意思是,我可以看到在数据库以外的东西上使用类似SQL的语句的好处。 但是,如果我想编写SQL,那么,为什么不编写SQL并将其保留在C#之外呢? 我在这里想念的是什么?

LINQ不是关于SQL的。 LINQ是关于在对象上应用函数式编程范例。

LINQ to SQL是一个构建在LINQ基础之上的ORM,但LINQ更多。 我不使用LINQ to SQL,但我一直使用LINQ。

找到两个列表的交集任务:

在LINQ之前,这个任务需要编写一个嵌套的foreach,它为大列表O(N * M)中的每个项迭代一次小列表,并且需要大约10行代码。

foreach (int number in list1) { foreach (int number2 in list2) { if (number2 == number) { returnList.add(number2); } } } 

使用LINQ,它在一行代码中做同样的事情:

 var results = list1.Intersect(list2); 

您会注意到它看起来不像LINQ,但它确实如此。 如果您不想,则不需要使用表达式语法。

之前:

 // Init Movie m_ImageArray = new Image[K_NB_IMAGE]; Stream l_ImageStream = null; Bitmap l_Bitmap = null; // get a reference to the current assembly Assembly l_Assembly = Assembly.GetExecutingAssembly(); // get a list of resource names from the manifest string[] l_ResourceName = l_Assembly.GetManifestResourceNames(); foreach (string l_Str in l_ResourceName) { if (l_Str.EndsWith(".png")) { // attach to stream to the resource in the manifest l_ImageStream = l_Assembly.GetManifestResourceStream(l_Str); if (!(null == l_ImageStream)) { // create a new bitmap from this stream and // add it to the arraylist l_Bitmap = Bitmap.FromStream(l_ImageStream) as Bitmap; if (!(null == l_Bitmap)) { int l_Index = Convert.ToInt32(l_Str.Substring(l_Str.Length - 6, 2)); l_Index -= 1; if (l_Index < 0) l_Index = 0; if (l_Index > K_NB_IMAGE) l_Index = K_NB_IMAGE; m_ImageArray[l_Index] = l_Bitmap; } l_Bitmap = null; l_ImageStream.Close(); l_ImageStream = null; } // if } // if } // foreach 

后:

 Assembly l_Assembly = Assembly.GetExecutingAssembly(); //Linq is the tops m_ImageList = l_Assembly.GetManifestResourceNames() .Where(a => a.EndsWith(".png")) .OrderBy(b => b) .Select(c => l_Assembly.GetManifestResourceStream(c)) .Where(d => d != null) //ImageStream not null .Select(e => Bitmap.FromStream(e)) .Where(f => f != null) //Bitmap not null .ToList(); 

或者,( 查询语法 ):

 Assembly l_Assembly = Assembly.GetExecutingAssembly(); //Linq is the tops m_ImageList = ( from resource in l_Assembly.GetManifestResourceNames() where resource.EndsWith(".png") orderby resource let imageStream = l_Assembly.GetManifestResourceStream(resource) where imageStream != null let bitmap = Bitmap.FromStream(imageStream) where bitmap != null) .ToList(); 

因此,关于LINQ的真正非常重要的事情与Linq to SQL无关。 它是关于它为C#语言本身带来的增强function。

LINQ不仅仅是一个ORM系统,因为Jonathan指出它为C#带来了许多函数式编程元素。 它允许您在常规C#代码中执行大量“数据库”操作。 很难解释这可能是多么强大。 考虑在通用框架中包含的可靠,精心设计的通用数据结构(例如列表,堆栈,字典/散列等)有多少改善了现代语言的开发状态。 正是因为使用这些数据结构非常普遍,减少使用它们的智力开销是一个巨大的好处。 LINQ不会做任何你自己无法做的事情,但它使很多操作变得更简单,更容易。

考虑从非有序列表中删除重复项的历史悠久的示例。 在较低级别的语言(如C或C ++)中,您可能需要对列表进行排序,并在删除dupes时将两个索引保存到列表中。 在具有散列(Java,C#,Javascript,Perl等)的语言中,您可以创建散列,其中键是唯一值,然后将键提取到新列表中。 使用LINQ你可以这样做:

 int[] data = { 0, 1, 3, 3, 7, 8, 0, 9, 2, 1 }; var uniqueData = data.GroupBy(i => i).Select(g => g.Key); 

因为linq在sql服装中真的是monad,所以我在一个项目中使用它来使用continuation monad进行异步Web请求,并且它certificate工作得非常好!

看看这些文章: http: //www.aboutcode.net/2008/01/14/Async+WebRequest+Using+LINQ+Syntax.aspx http://blogs.msdn.com/wesdyer/archive/2008/01/ 11 /在-奇迹-的-monads.aspx

从第一篇文章:

  var requests = new[] { WebRequest.Create("http://www.google.com/"), WebRequest.Create("http://www.yahoo.com/"), WebRequest.Create("http://channel9.msdn.com/") }; var pages = from request in requests select from response in request.GetResponseAsync() let stream = response.GetResponseStream() from html in stream.ReadToEndAsync() select new { html, response }; foreach (var page in pages) { page(d => { Console.WriteLine(d.response.ResponseUri.ToString()); Console.WriteLine(d.html.Substring(0, 40)); Console.WriteLine(); }); } 

关键是LINQ将您的查询集成到您的主要编程语言中,允许您的IDE为您提供一些您不具备的function(例如,智能感知和调试支持),并允许编译器对您的SQL进行类型检查代码(使用普通字符串查询是不可能的)。