LInq 使用where过滤,匿名类型来查找想要的数据。


 

View Code
 static void Main(string[] args)
        {
            List
<Person> personL = new List<Person>();
            personL.Add(
new Person("wtq""""13616009873"23));
            personL.Add(
new Person("wta""""13616009871"24));
            personL.Add(
new Person("wtb""""13616009872"26));
            personL.Add(
new Person("wtc""""13616009873"25));
            personL.Add(
new Person("wtd""""13616009874"27));
            personL.Add(
new Person("wte""""13616009875"28));
            personL.Add(
new Person("wtf""""13616009876"29));
            personL.Add(
new Person("wtg""""13616009877"30));
            personL.Add(
new Person("wth""""13616009878"23));

            var pr 
= from n in personL
                     
where n.Age == 23
                     select 
new { myname = n.Name, mysex = n.Sex };

            
foreach (var item in pr)
            {
                Console.WriteLine(item.myname
+" "+item.mysex);
            }

            Console.ReadLine();

        }

 使用foreach进行遍历数据。

 

在使用匿名类型时,可以不指定名称,而是使用元数据的字段名,如下代码:

View Code
 var pr = from n in personL
                     
where n.Age == 23
                     select 
new { n.Name, n.Age };

 

posted on 2011-07-30 16:15  wtq  阅读(867)  评论(0编辑  收藏  举报