如何使用Lambda表达式循环和比较2个列表

时间:2014-11-19 05:24:21

标签: c# lambda

假设我有一个名为Features

的类
class Features
{
    public Features()
    {
    }
    public Features(string id, string name)
    {
        this.FeatureID = id; this.FeatureName = name;
    }
    private string featureid;
    private string featurename;
    public string FeatureID
    {
        get { return featureid; }
        set { featureid = value; }
    }
    public string FeatureName
    {
        get { return featurename; }
        set { featurename = value; }
    }
}

然后我创建了2个与以下相同类型的列表:

List<Features> list1 = new List<Features>();
list1.Add(new Features("1111111111", "Feature 1"));
list1.Add(new Features("2222222222", "Feature 2"));
list1.Add(new Features("3333333333", "Feature 3"));
list1.Add(new Features("4444444444", "Feature 4"));
list1.Add(new Features("5555555555", "Feature 5"));

List<Features> list2 = new List<Features>();
list2.Add(new Features("1111111111", "Feature 1"));
list2.Add(new Features("0002222222", "Feature 2"));
list2.Add(new Features("0003333333", "Feature 3"));
list2.Add(new Features("0004444444", "Feature 4"));
list2.Add(new Features("5555555555", "Feature 5"));

然后我使用lambda表达式对这两个列表进行了比较,如下所示:

var newList = list1
.Select(
x => (new Features
{
FeatureID = x.FeatureID,
FeatureName = x.FeatureName
}
)
).Where(t=> t.FeatureID == list2.FirstOrDefault().FeatureID ).ToList();

newList.ForEach(t => Console.WriteLine(t.FeatureName));

到目前为止,此代码仅返回两个列表中匹配的第一个功能ID ...

问题是:

如何使用Lambda表达式在两个列表上循环?我已经尝试了Any和All,但是没有任何效果,但FirstOrDefault()如上所示..

非常感谢, 赞赏。

3 个答案:

答案 0 :(得分:1)

试试这个

var result= from x1 in list1
            join x2 in list2 on x1.FeatureID  equals x2.FeatureID 
            select x1;

答案 1 :(得分:0)

非常感谢!我用Expressions :)得到了它。

var result = list1.Join(list2, t => t.FeatureID, t => t.FeatureID, (x, y) => new
        {
            ID = x.FeatureID,
            Name = x.FeatureName
        }).ToList();
result.ForEach(t => Console.WriteLine(t.Name));

答案 2 :(得分:0)

你基本上在LINQ中寻找Left Join: -

var result1 = list1.GroupJoin(list2,
                              l1 => l1.FeatureID,
                              l2 => l2.FeatureID,
                              (x, y) => new { x, y })
                                        .SelectMany(z => z.y.DefaultIfEmpty(),
                                        (a, b) => new
                                        {
                                             FeatureName = b == null ? "Feature don't match" : b.FeatureName
                                        });

左连接的Lamda表达式有点复杂,您也可以使用查询语法: -

var result = from l1 in list1
                      join l2 in list2
                      on l1.FeatureID equals l2.FeatureID into allFeatures
                      from finalList in allFeatures.DefaultIfEmpty()
                      select new
                        {
                           FeatureName = finalList == null ? "Feature don't match" : finalList.FeatureName
                        };

这给了我这个输出: -

Feature1
Feature don't match
Feature don't match
Feature don't match
Feature5