[英]IGrouping <decimal,string> does not contain a defintion for 'name'
我正在尝试使用LINQ和lambda表达式将提取到的数据写入文本文件。
using (var contextDb = new TimoToolEntities())
{
using (var writeFile = new StreamWriter(saveTo))
{
var randomData = contextDb.WorkCenter_Operations.Where(d => d.Job_Number >= 1 && d.Part_Number.Length >= 1 && d.Oper_Number >= 1 )
.OrderBy(d => d.Oper_Number)
.GroupBy(d => d.Job_Number , d => d.Part_Number ).ToList();
foreach (var record in randomData)
{
Console.WriteLine(record.Job_Number + "," + record.Part_Number); // error here
}
}
Console.ReadLine();
}
我收到错误消息,“ IGrouping不包含对“ name”的定义,并且找不到可以接受类型为“ IGrouping”的第一个参数的扩展方法“ name”。
我环顾四周,并认为这些对象是匿名的,但是我找不到能够起作用的修复程序。
当您使用GroupBy
此重载时
.GroupBy(d => d.Job_Number , d => d.Part_Number )
第一个lambda是键选择器(由Job_Number
),第二个是值选择器。 您的record
将Part_Number
Job_Number
为键的Part_Number
的集合。
此MSDN示例说明了基本用法:
// Group the pets using Age as the key value
// and selecting only the pet's Name for each value.
IEnumerable<IGrouping<int, string>> query =
pets.GroupBy(pet => pet.Age, pet => pet.Name);
// Iterate over each IGrouping in the collection.
foreach (IGrouping<int, string> petGroup in query)
{
// Print the key value of the IGrouping.
Console.WriteLine(petGroup.Key);
// Iterate over each value in the
// IGrouping and print the value.
foreach (string name in petGroup)
Console.WriteLine(" {0}", name);
}
您的意图不是100%清楚的,因此,以防万一您实际上想按多个字段分组时,请使用不同的重载,如下所示:
.GroupBy(d => new { d.Job_Number, d.Part_Number })
然后您的record
将是您的所有数据的集合,并将具有一个匿名密钥,您可以在其中访问例如record.Key.Job_Number
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.