我是否缺少有关LINQ的内容?
我似乎缺少有关LINQ的东西。对我来说,似乎正在吸收我最不喜欢的某些SQL元素,并将其移入Clanguage,并将其用于其他用途。
我的意思是,我可以看到在数据库以外的其他事物上使用类似SQL的语句的好处。但是,如果我想编写SQL,为什么不只编写SQL并将其排除在C#之外呢?我在这里想念什么?
解决方案
回答
关键是LINQ将查询集成到主要编程语言中,从而使IDE可以为我们提供我们原本不具备的一些功能(例如,智能感知和调试支持),并允许编译器对SQL进行类型检查。代码(对于普通的字符串查询是不可能的)。
回答
LINQ与SQL无关。 LINQ即将在对象上应用函数式编程范例。
LINQ to SQL是在LINQ基础之上构建的ORM,但是LINQ还有很多。我不使用LINQ to SQL,但是一直使用LINQ。
承担查找两个列表的交集的任务:
在LINQ之前,此任务需要编写一个嵌套的foreach,该嵌套的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,但是确实如此。如果不想,则不需要使用表达式语法。
回答
因此,关于LINQ的非常重要的事情与Linq to SQL无关。这是关于它为语言本身带来的增强。
回答
乔纳森(Jonathan)指出,LINQ不只是一个ORM系统,它为C#带来了许多功能性编程元素。它使我们可以在常规Ccode中执行很多"数据库化"的事情。很难解释这到底有多强大。考虑一下通用框架中包含的坚固,精心设计的通用数据结构(例如列表,堆栈,字典/哈希等)在多大程度上改善了现代语言的发展状况。正是因为使用这些数据结构非常普遍,并且减少使用它们的知识开销是一个巨大的好处。 LINQ不会做我们无法做的任何事情,但是它使很多操作更加直接和容易。
考虑一下从无序列表中删除重复项的历史悠久的示例。在使用较低级别的语言(例如C或者C ++)时,我们可能必须在删除重复项时对列表进行排序并在列表中维护两个索引。在具有散列的语言(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);
回答
前:
// 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实际上是sql服装中的monad,所以我在一个项目上使用linq来使用延续monad发出异步Web请求,事实证明它工作得很好!
查看以下文章:
http://www.aboutcode.net/2008/01/14/Async+WebRequest+Using+LINQ+Syntax.aspx
http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-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(); }); }