1. 实体类可以这样定义
public class PersonInfo
    {
        
public bool IsOnline { getset; }
        
public string Name { getset; }
        
public int Id { getset; }
    }


2. 数组可以这样操作
string[] words = { "hello""wonderful""linq""beautiful""world" };

    
// Get only short words
    var shortWords =
      from word 
in words
      
where word.Length <= 5
      select word;

    
// Print each word out
    foreach (var word in shortWords)
      Console.WriteLine(word);


3. 匿名类型(anonymous type)
var books = new[] {
      
new {Title="Ajax in Action", Publisher="Manning", Year=2005 },
      
new {Title="Windows Forms in Action", Publisher="Manning", Year=2006 },
      
new {Title="RSS and Atom in Action", Publisher="Manning", Year=2006 }
    };


4. XML可以这样操作
//对象books在3中初始化
XElement xml = new XElement("books",
      from book 
in books
      
where book.Year == 2006
      select 
new XElement("book",
        
new XAttribute("title", book.Title),
        
new XElement("publisher", book.Publisher)
      )
    );


5. 支持比较复杂的分组操作
string[] words = { "hello""wonderful""linq""beautiful""world" };

    
// Group words by length
    var groups =
      from word 
in words
      orderby word ascending
      group word by word.Length into lengthGroups
      orderby lengthGroups.Key descending
      select 
new { Length = lengthGroups.Key, Words = lengthGroups };

    
// Print each group out
    foreach (var group in groups)
    {
      Console.WriteLine(
"Words of length " + group.Length);
      
foreach (string word in group.Words)
        Console.WriteLine(
"  " + word);
    }



http://code.google.com/p/linqinaction-csharp-sample/

相关文章:

  • 2022-01-28
  • 2021-06-03
  • 2021-11-20
  • 2021-09-15
  • 2021-05-25
  • 2022-12-23
猜你喜欢
  • 2022-02-19
  • 2021-12-15
  • 2023-03-20
  • 2021-06-26
  • 2021-04-06
  • 2021-07-17
相关资源
相似解决方案