[英]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.