【问题标题】:Linq - how to replace a lists property with value from another list where both keys are equalLinq - 如何用两个键相等的另一个列表中的值替换列表属性
【发布时间】:2017-03-30 01:26:15
【问题描述】:

我有 2 个列表 groupoptions 和 dataIndicator

class groupoptions{
  ...
  GroupName string
}

class dataIndicator{
  ...
 HeaderID int
 IndicatorDescription
}

目前我的组名可能值为 4 或 5 或 11

首先我想获取所有 headerid 等于 GroupName 的 dataindicators,然后用 dataindicator 的 IndicatorDescription 替换这些 GroupNames

Linq 的语法是什么?

更新

使用连接

var newList = from first in dataIndicator
                      join second in groupedoptions
                      on first.HeaderID.ToString() equals second.GroupName
                      select new { first,second };

接下来呢? 问题:我想在 Linq Select 中创建的构造函数中执行此操作

var list = xyz.Select(x => new groupoptions(){
   GroupName = x.Key.ToString();
 })

【问题讨论】:

  • 您尝试了哪些方法,但以何种方式不起作用?
  • 我正在使用 dataIndicator.GetEnumerator 但想要一/两行 linq 语法
  • 尚未使用(getenumerator)
  • 能否提供有效的 C# 代码和一些示例数据(也在 C# 中)?
  • 代码已更新,如果您需要其他内容,请告诉我

标签: c# linq


【解决方案1】:

这个小 for 循环只需几行就可以完成您想要做的事情。如果您只想提高效率,则无需使用 linq。

class ConsoleApplication1
{
    public static void Main()
    {

        List<groupOptions> g = new List<groupOptions>();
        List<dataIndicator> d = new List<dataIndicator>();

        for (int i = 0; i < 4; i++)
        {
            g.Add(new groupOptions() { groupName = i.ToString() });
            d.Add(new dataIndicator() { headerID = i, indicatorDescription = "id:" + i});
            Console.Write(g[i].groupName + ":");
            Console.WriteLine(d[i].headerID);
        }

        Console.WriteLine("enter to change");
        Console.ReadLine();

这是相关部分:

        for(int i = 0; i < g.Count(); i++)
        {
            if (g[i].groupName == d[i].headerID.ToString())
                g[i].groupName = d[i].indicatorDescription;
        }

剩下的只是为了确保它有效。

        for(int i = 0; i < d.Count(); i++)
        {
            Console.WriteLine(g[i].groupName);
        }

        Console.ReadLine();
    }
}

public class groupOptions
{

    public string groupName { get; set; }

}


class dataIndicator
{
    public int headerID { get; set; }
    public string indicatorDescription { get; set; }
}

【讨论】:

    【解决方案2】:

    我的问题是我想在 Linq Select 中创建的构造函数中执行它...我做了以下而不是加入

    var list = xyz.Select(x => new groupoptions()
            {      
              GroupName = dataIndicator.Where(d => d.IndicatorID.ToString().Equals(x.Key.ToString())).First().IndicatorDescription
                }
    

    附:第一个是因为总会有一个匹配的记录..而且当我应该与 indicatorid 属性进行比较时,我在与 headerid 进行比较时犯了一个错误。

    【讨论】:

      【解决方案3】:

      这是一个非 linq 示例:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      
      namespace ConsoleApplication5
      {
          class Program
          {
              static void Main(string[] args)
              {
                  var groupOptions = new List<GroupOption>();
                  groupOptions.Add(new GroupOption { GroupName = 4.ToString() });
                  groupOptions.Add(new GroupOption { GroupName = 5.ToString() });
                  groupOptions.Add(new GroupOption { GroupName = 11.ToString() });
      
                  var dataIndicators = new List<DataIndicator>();
                  dataIndicators.Add(new DataIndicator { HeaderID = 4, IndicatorDescription = "four" });
                  dataIndicators.Add(new DataIndicator { HeaderID = 99, IndicatorDescription = "ninetynine" });
                  dataIndicators.Add(new DataIndicator { HeaderID = 100, IndicatorDescription = "onehundred" });
      
                  Console.WriteLine("Before:");
                  PrintGroupOptions(groupOptions);
      
                  foreach (var dataIndicator in dataIndicators)
                  {
                      foreach (var groupOption in groupOptions)
                      {
                          if (dataIndicator.HeaderID.ToString() == groupOption.GroupName)
                          {
                              groupOption.GroupName = dataIndicator.IndicatorDescription;
                          }
                      }
                  }
      
                  Console.WriteLine("After:");
                  PrintGroupOptions(groupOptions);
              }
      
              static void PrintGroupOptions(List<GroupOption> groupOptions)
              {
                  groupOptions.ToList().ForEach(g => Console.WriteLine(g));
              }
          }
      
          class GroupOption
          {
              public string GroupName { get; set; }
              public override string ToString()
              {
                  return GroupName;
              }
          }
      
          class DataIndicator
          {
              public int HeaderID { get; set; }
              public string IndicatorDescription { get; set; }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-06
        • 2020-10-23
        • 1970-01-01
        • 2014-06-23
        • 1970-01-01
        • 2020-03-20
        • 1970-01-01
        • 2023-02-05
        • 2019-09-01
        相关资源
        最近更新 更多