对于LINQ的初学者我们经常会遇到书写细节上的错误,那么就像LINQ嵌套的实际操作过程中,我们可能会出现输出的结果不是我们想要的,那么对于LINQ的使用要注意什么细节呢?让我们看看一个例子。
LINQ嵌套的实例:
- using System;
- using System.Linq;
- class P
- {
- static void Main()
- {
- var customer = new[]{new
- {PKId="001",Name="admin",Pwd="123456",Group="001"},
- new
- {PKId="002",Name="user",Pwd="user",Group="003"},
- new
- {PKId="003",Name="geust",Pwd="guest",Group="004"},
- new
- {PKId="004",Name="aa",Pwd="123456",Group="002"},
- new
- {PKId="005",Name="bb",Pwd="123456",Group="002"},
- new
- {PKId="006",Name="cc",Pwd="123456",Group="002"},
- };
- var groups = new[] { new { PKId = "001",
- depict = "***权限" },
- new
- {PKId="002",depict="管理员"},
- new
- {PKId="003",depict="发布信息"},
- new
- {PKId="004",depict="只有浏览权限"}
- };
- var data =
- from word2 in groups
- where word2.depict == "管理员"
- //LINQ嵌套的问题所在
- select new {
- quanxian = word2.depict,
- Name =
- from word in customer
- where word.Group==word.PKId
- select word
- };
- var first = data.First();
- Console.WriteLine("{0}, {1}",
- first.quanxian, first.Name);
- }
- }
- //下面是输出:
- 管理员, System.Linq.Enumerable+
- WhereArrayIterator`1[<>f__AnonymousType0`4[System
- .String,
- System.String,System.String,System.String]]
关于LINQ嵌套问题的分析:
看到没?Name的值是个其名IEnumerator.
正确的LINQ嵌套写法应该是:
- var data =
- from word2 in groups
- where word2.depict == &
LINQ嵌套的实战分析就向你介绍到这里,希望对你了解和学习LINQ嵌套有所帮助。
【编辑推荐】