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

本文关键字:比较 列表 循环 何使用 Lambda 表达式 | 更新日期: 2023-09-27 18:31:00

假设我有一个名为 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 表达式在两个列表上循环?我已经尝试了任何和所有,但除了 FirstOrDefault() 之外没有任何效果,如上所示。

多谢赞赏。

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

试试这个

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

非常感谢大家!!我使用表达式得到它:)

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

你基本上是在 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