[英]IGrouping <decimal,string> does not contain a defintion for 'name'
Using LINQ and lambda expressions, I am trying to write data that I have pulled to a text file. 我正在尝试使用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();
}
I am getting the error the 'IGrouping does not contain a definition for 'name' and no extension method 'name' accepting a first argument of type 'IGrouping' could be found. 我收到错误消息,“ IGrouping不包含对“ name”的定义,并且找不到可以接受类型为“ IGrouping”的第一个参数的扩展方法“ name”。
I have looked around and believe that the objects are anonymous, but I haven't been able to find a fix that will work. 我环顾四周,并认为这些对象是匿名的,但是我找不到能够起作用的修复程序。
When you use this overload of GroupBy
当您使用GroupBy
此重载时
.GroupBy(d => d.Job_Number , d => d.Part_Number )
the first lambda is a key selector (you group by Job_Number
) and the second one is a value selector. 第一个lambda是键选择器(由Job_Number
),第二个是值选择器。 Your record
will be a collection of Part_Number
with Job_Number
as a key. 您的record
将Part_Number
Job_Number
为键的Part_Number
的集合。
This MSDN example illustrates the basic usage: 此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);
}
Your intent is not 100% clear, so just in case you actually wanted to group by multiple fields, use a different overload like this: 您的意图不是100%清楚的,因此,以防万一您实际上想按多个字段分组时,请使用不同的重载,如下所示:
.GroupBy(d => new { d.Job_Number, d.Part_Number })
Then your record
will be a collection of whatever your data is and will have an anonymous key where you can access for example record.Key.Job_Number
然后您的record
将是您的所有数据的集合,并将具有一个匿名密钥,您可以在其中访问例如record.Key.Job_Number
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.