简体   繁体   English

IGrouping <decimal,string> 不包含“名称”的定义

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

相关问题 IGrouping不包含以下定义 - IGrouping does not contain a definition for 分组 <anonymous type:, string> 不包含“”的定义,也没有扩展方法“” - IGrouping<anonymous type:, string> does not contain a definition for '' and no extension method '' IServiceCollection不包含AddHttpClient的定义 - IServiceCollection does not contain a defintion for AddHttpClient “KeyPressEventArgs”不包含 KeyCode 错误的定义 - 'KeyPressEventArgs' does not contain defintion for KeyCode error Visual Studio 2015存储库模式不包含以下定义 - Visual Studio 2015 repository pattern does not contain defintion for System.Web.WebPages.Html.HtmlHelper不包含定义 - System.Web.WebPages.Html.HtmlHelper does not contain a defintion 分组<int, Customer>不包含点等的定义 - IGrouping <int, Customer> does not contain a definition for Points etc &#39;decimal&#39;不包含&#39;Round&#39;的定义 - 'decimal' does not contain a definition for 'Round' 数据绑定:“ System.String”不包含名称为“ Name”的属性 - DataBinding: 'System.String' does not contain a property with the name 'Name' LINQ:方法&#39;GroupBy&#39;的重载没有6个参数/ IGrouping <t,t> 不包含定义 - LINQ: No overload for method 'GroupBy' takes 6 arguments / IGrouping<t,t> does not contain a definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM