日期:2014-05-20  浏览次数:20816 次

初学LinQ不懂,请教分析下,在线谢谢.
Assume the following namespaces are imported: 
using System; 
using System.Linq; 
using System.Data.Linq; 
using System.Xml.Linq; 
using System.Collections; 
 
and that the following array is defined: 
string[] colors = { "green", "brown", "blue", "red" }; 
  
What is the output from the following? 
var query = 
  from c in colors 
  where c.Length == colors.Max (c2 => c2.Length) 
  select c; 
  
foreach (var element in query) 
  Console.WriteLine (element); 


------解决方案--------------------
不就是一个 linq查询,然后再输出出来.

不晓得你想分析什么?
------解决方案--------------------
这个Linq查询的意思是

首先找出 color array中长度最长的值,就是这条语句
colors.Max (c2 => c2.Length)

然后找出color array中长度和这个值一样的所有string

就是这么个意思,很简单

------解决方案--------------------
Min - 返回集合的最小值;不延迟
Max - 返回集合的最大值;不延迟

长度最大的值
输出就知道了
参考
------解决方案--------------------
你要是有点Sql知识就好理解了。

var query = 
from c in colors //从Colors里面进行选择
where c.Length == colors.Max (c2 => c2.Length) //选出哪些长度等于最大长度的项
select c; //返回的集合中的项。

而LinQ实际上没有啥新东西,只是匿名类,匿名delegate的简化写法而已。
上面的LinQ语句可以翻译成一长串使用匿名delegate的等值语句。

要想了解参考
Professional C# 2008 的第11章 Language Integrated Query.