【问题标题】:C# LINQ - Split Rows into Two RowsC# LINQ - 将行拆分为两行
【发布时间】:2018-10-20 08:18:57
【问题描述】:

很抱歉这个可能很愚蠢的问题,但我将这个 Ienumerable<> 设置为这样

我需要将名称和百分比拆分为单独的行,并为这些行复制 ProductId 和 RowIndex(我知道它效率不高,但这是需要做的)。并且可能是一个新字段来指定组合行具有哪些数据。

例如,

ProductId, Name , Percent, RowIndex 2301283 , PLACEHOLDER, 12.20 , 1

应该变成这样:

ProductId, DataType, Value , RowIndex 2301283 , Name , PLACEHOLDER, 1 2301283 , Percent , 12.20 , 1 etc etc

此外,如果有意义的话,它们不能嵌套在其他列表或可枚举或任何东西中。

在 LINQ 中可以吗?

【问题讨论】:

  • 您需要单个结果序列,还是必须有两个序列:第一个带有名称,第二个带有百分比?如果是单序列描述您想要包含数据的数据类型。
  • .Select(entireRow => Tuple.Create(entireRow.Left, entireRow.Right).UnZip();
  • @SeM 要达到上表中显示的 LINQ 数据本身需要很长时间......

标签: c# mysql linq


【解决方案1】:
//list is your data collection
//newlist is the result
var newlist = new List<dynamic>();
list.ForEach(w =>
{
    newlist.Add(
        new {w.ProductID,DataType="Percent",Value=w.Percent,w.RowIndex}
    );
    newlist.Add(
        new {w.ProductID,DataType="Name",Value=w.Name,w.RowIndex}
    );
});
newlist.Dump();

希望对你有帮助:-)

【讨论】:

  • 您在这里列举了两次列表。因为 .Select 实际上是一个仿函数,select (g) . select(f) 总是等于 select(g . f) 的两倍(在最坏情况下完成的操作)。
  • EmrahSüngü Süngü,Sereja Bogolubov 谢谢,我修好了
【解决方案2】:

据我所知,您希望将每个 GI 对象转换为 2 个新对象,并将所有这些对展平为一个可枚举的对象。希望我理解正确。

我认为SelectMany 可以胜任。

yourEnumerable.SelectMany(x => new[] {
    new { ProductId = x.ProductId, DataType = "Name", Value = x.Name, RowIndex = x.RowIndex },
    new { ProductId = x.ProductId, DataType = "Percent", Value = x.Percent, RowIndex = x.RowIndex }
})

【讨论】:

  • 这个好像搞定了,太感谢了!这给我带来了几个小时的问题!
【解决方案3】:

您可以使用 LINQ .Select 方法和一个新类。为了有效地做到这一点,你可以使用Tuple&lt;T1, T2&gt; 结构:

public class AttributeData
{
    public int ProductId { get; set; }
    public string DataType { get; set; }
    public string Value { get; set; }
    public int RowIndex { get; set; }
}

yourEnumerable.Select(x => 
     Tuple.Create(
         new AttributeData
         { 
            ProductId = x.ProductId,
            DataType = "Name",
            Value= x.Name,
            RowIndex= x.RowIndex
         },
         new AttributeData
         { 
            ProductId = x.ProductId,
            DataType = "Percent",
            Value= x.Percent,
            RowIndex= x.RowIndex
         }
      ).ToList();

【讨论】:

    【解决方案4】:

    这样试试

    这将使用SelectMany 得到平坦的结果

       var data = (from entity in Entities
        select new [] {
             new { entity.ProductId, DataType = "Name", Value= entity.Name, entity.RowIndex},
             new { entity.ProductId, DataType = "Percentage", Value= entity.Percentage, entity.RowIndex},
        }).SelectMany(e=> e);
    

    这将为您提供列表,其中每个元素都包含 Name 和 Percetnage 行

       var data = (from entity in Entities
        select  new {
             NameEntity = new { entity.ProductId, DataType = "Name", Value= entity.Name, entity.RowIndex},
             PercentageEntity = new { entity.ProductId, DataType = "Percentage", Value= entity.Percentage, entity.RowIndex},
        });
    
      foreach(var d in data) {
        //d.NameEntity
        //d.PercentageEntity  
      }
    

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多