2017-03-30 28 views
0

我有2个列出groupoptions和dataIndicatorLINQ的 - 如何从另一个列表中其中两个密钥是相同的替换值列表属性

class groupoptions{ 
    ... 
    GroupName string 
} 

class dataIndicator{ 
    ... 
HeaderID int 
IndicatorDescription 
} 

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

首先我想要得到所有的数据,其中headerid等于GroupName,然后用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 }; 

下一步

UPDATE

? 问题:我想做到这一点正被一个LINQ中创建一个构造函数中选择

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

你尝试过什么,以何种方式是不工作? –

+0

我正在使用dataIndicator.GetEnumerator,但想要一个/两行LINQ语法 – Samra

+0

尚未使用(getenumerator)但 – Samra

回答

0

我的问题是我想做到这一点正被一个LINQ选择内部创建一个构造函数里面......我做了以下,而不是加入

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

附:首先是因为总会有一个匹配的记录。当我比较指示性属性时,我还犯了一个与headerid比较的错误。

1

这个小的循环可以让你在找什么,在短短的几行做完成。如果你所要做的只是有效的话,不需要使用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; } 
} 
0

这里有一个非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; } 
    } 
} 
相关问题