c#关于两个list去对比,比较不同,list结构为姓名,年龄,比较逻辑为当姓名相同,就比较年龄,取出他们的不同
你的最新需求已改好,我输出了编码相同但坐标不同,仅供参考,望采纳:
实体类:
internal class Model
{
public string X { get; set; }
public string Y { get; set; }
public Model(string X, string Y)
{
this.X = X;
this.Y = Y;
}
}
Main方法:
Dictionary<string, Model> a = new Dictionary<string, Model>();
Dictionary<string, Model> b = new Dictionary<string, Model>();
a.Add("a01", new Model("a0", "a1"));
a.Add("a02", new Model("a1", "a2"));
a.Add("a03", new Model("a2", "a3"));
b.Add("a01", new Model("a0", "a2"));
b.Add("b02", new Model("a1", "a2"));
b.Add("a02", new Model("a1", "a3"));
foreach (var i in a)
{
foreach(var j in b)
{
if(i.Key == j.Key)
{
if(i.Value.X != j.Value.X || i.Value.Y != j.Value.Y)
{
Console.WriteLine(i.Key + "编码相同,坐标分别为(" + i.Value.X+","+i.Value.Y + "),(" + j.Value.X + "," + j.Value.Y + ")");
}
}
}
}
可以使用 Linq 查询来实现这个功能。需要先将两个 list 中的元素进行比较。如果两个元素的姓名不相同,就可以直接将其加入结果集中。如果姓名相同,就比较年龄,如果年龄不同,就将这两个元素加入结果集中。
可以使用以下代码来实现这个功能:
using System.Linq;
List<Person> list1 = // 初始化 list1
List<Person> list2 = // 初始化 list2
// 比较两个 list 中的元素
var differentElements = list1.Union(list2).Where(x => !list1.Contains(x) || !list2.Contains(x)).ToList();
// 定义 Person 类
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public override bool Equals(object obj)
{
var person = obj as Person;
return person != null &&
Name == person.Name &&
Age == person.Age;
}
}
仅供参考,望采纳,谢谢。
在 C# 中,你可以使用 LINQ 查询来对比两个 List,并取出它们的不同。
下面代码示例定义了一个 Person 类,其中包含姓名和年龄两个字段,然后使用 LINQ 查询对比两个 List:
using System;
using System.Linq;
using System.Collections.Generic;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Person> list1 = new List<Person>
{
new Person { Name = "Tom", Age = 10 },
new Person { Name = "Jerry", Age = 20 },
new Person { Name = "Mike", Age = 30 }
};
List<Person> list2 = new List<Person>
{
new Person { Name = "Tom", Age = 10 },
new Person { Name = "Jerry", Age = 25 },
new Person { Name = "Marry", Age = 30 }
};
// 使用 LINQ 查询,比较两个 List 的不同
var different = list1.Except(list2, new PersonComparer())
.Union(list2.Except(list1, new PersonComparer()));
// 遍历比较结果
foreach (var person in different)
{
Console.WriteLine(person.Name + " " + person.Age);
}
}
}
【望采纳】
1、直接Linq进行比较
List<StudentModel> oneList = new List<StudentModel>();
oneList.Add(new StudentModel() { name = "张三", age = 23 });
oneList.Add(new StudentModel() { name = "李四", age = 25 });
oneList.Add(new StudentModel() { name = "王五", age = 26 });
List<StudentModel> twoList = new List<StudentModel>();
twoList.Add(new StudentModel() { name = "李四2", age = 25 });
twoList.Add(new StudentModel() { name = "王五3", age = 26 });
twoList.Add(new StudentModel() { name = "张三", age = 26 });
List<StudentModel> three = new List<StudentModel>();
foreach(var item in oneList)
{
var dataObj = twoList.Where(a => a.name == item.name && a.age != item.age).ToList();
if (dataObj != null && dataObj.Count > 0)
{
three.Add(dataObj.First());
three.Add(item);
}
}
public class StudentModel
{
public string name { get; set; }
public int age { get; set; }
}
你说的是python吗,c#里一个list中怎么又存string又存int,你到底定义了个什么list,代码放出来瞧瞧