我有2个列表groupoptions和dataIndicator
class groupoptions{
...
GroupName string
}
class dataIndicator{
...
HeaderID int
IndicatorDescription
}
目前我的groupname可能值为4或5或11
首先我想得到所有数据绑定器,其中headerid等于GroupName,然后将这些GroupNames替换为dataindicator' s IndicatorDescription
Linq的语法是什么?
更新
使用join
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();
})
答案 0 :(得分: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; }
}
答案 1 :(得分:0)
我的问题是我想在Linq Select中创建的构造函数中执行...我执行了以下操作而不是join
var list = xyz.Select(x => new groupoptions()
{
GroupName = dataIndicator.Where(d => d.IndicatorID.ToString().Equals(x.Key.ToString())).First().IndicatorDescription
}
P.S。首先是因为总会有一条匹配的记录。当我应该与indicatorid属性进行比较时,我还错误地与headerid进行比较。
答案 2 :(得分: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; }
}
}