简体   繁体   English

LINQ Expression访问更多字段

[英]LINQ Expression accessing more fields

I am really new to using LINQ and I was wondering what I need to do to the below expression to grab extra fields 我真的是使用LINQ的新手,我想知道我需要对以下表达式进行操作以获取更多字段

    public class Foo
    {
        public string Name {get;set;}
        public string Manufacturer {get;set;}
        public float Price {get;set;}
    }

    var result= (
        from row in dt.AsEnumerable()
        group row by row.Field<string>("NAME") into g
        select new Foo
        {
            Name = g.Key,
            Price=g.Min (x =>x.Field<float>("PRICE"))
            //Manufacturer = ????
        }
    ).ToList();

I basically need to get the Manufacturer from the MANUFACTURER field and set it's value in the object. 我基本上需要从MANUFACTURER字段中获取Manufacturer并将其值设置在对象中。 I've tried: 我试过了:

    row.Field<string>("MANUFACTURER")
//and
    g.Field<string>("MANUFACTURER")

But I am having no luck accessing the field in the DataTable. 但是我没有运气来访问DataTable中的字段。 Can anyone advise what I'm doing wrong please? 谁能告诉我我在做什么错?

So you want to group by name. 因此,您想按名称分组。 But how do you want to aggregate the manufacturers for each name-group? 但是,您要如何汇总每个名称组的制造商?

Presuming that you just want to take the first manufacturer: 假设您只想选择第一家制造商:

var result= (
    from row in dt.AsEnumerable()
    group row by row.Field<string>("NAME") into g
    select new Foo
    {
        Name = g.Key,
        Price=g.Min (x =>x.Field<float>("PRICE")),
        Manufacturer = g.First().Field<string>("MANUFACTURER")
    }
).ToList();

Maybe you instead want to concatenate all with a separator: 也许您反而想用分隔符将所有内容连接起来:

// ...
Manufacturer = string.Join(",", g.Select(r=> r.Field<string>("MANUFACTURER")))

As your logic stands you may have more than one Manufacturer if you only group by Name. 按照您的逻辑,如果仅按名称分组,则可能有多个制造商。 To illustrate this consider the following data, which is supported by your data structure. 为了说明这一点,请考虑数据结构支持的以下数据。

Example

  • ProductA, ManufacturerA 产品A,制造商A
  • ProductA, ManufacturerB 产品A,制造商B

If you group by just "ProductA" then Manufacturer is a collection of ["ManufacturerA", "ManufacturerB"] 如果仅按“ ProductA”分组,则制造商是[“ ManufacturerA”,“ ManufacturerB”]的集合

Potential Solution 潜在解决方案

You could group by Name and Manufacturer then access both Name and Manufacturer 您可以按名称和制造商分组,然后访问名称和制造商

var result= (
  from row in dt.AsEnumerable()
  group row by new 
  { 
    row.Field<string>("NAME"), 
    row.Field<string>("MANUFACTURER") 
  }  into g
  select new Foo
  {
    Name = g.Key.Name,
    Manufacturer = g.Key.Manufacturer,
    Price=g.Min (x =>x.Field<float>("PRICE"))
  }
).ToList();

EDIT 编辑

Based on comment " I am trying to pull the name with the cheapest price and the manufacturer along with it. " 基于评论“ 我正在尝试以最便宜的价格和制造商来命名。

var result= (
  from row in dt.AsEnumerable()
  group row by row.Field<string>("NAME") into g
  let x = new
  {
    Name = g.Key.Name,
    Price=g.Min (x =>x.Field<float>("PRICE"))
  }
  where (row.Name == x.Name && row.Price == x.Price)
  select new Foo
  {
    Name = row.Name,
    Manufacturer = row.Manufacturer,
    Price= row.Price
  }
).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM