簡體   English   中英

Linq-如何用兩個鍵都相等的另一個列表中的值替換列表屬性

[英]Linq - how to replace a lists property with value from another list where both keys are equal

我有2個列表groupoptions和dataIndicator

class groupoptions{
  ...
  GroupName string
}

class dataIndicator{
  ...
 HeaderID int
 IndicatorDescription
}

目前我的群組名稱可能值為4或5或11

首先我想獲取headerid等於GroupName的所有dataindicator,然后將這些GroupName替換為dataindicator的IndicatorDescription

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();
 })

這個小的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; }
}

我的問題是我想在Linq Select中創建的構造函數中執行此操作...我執行了以下操作而不是加入

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

之所以選擇PS First,是因為總會有一條記錄匹配。.當我應該將它與indicatorid屬性進行比較時,我在與headerid進行比較時也犯了一個錯誤。

這是一個非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; }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM