using System;
using System.Collections.Generic;
using System.Linq;
public class Test
{
public class student
{
public int id{get;set;}
public string name{get;set;}
}
public static void Main()
{
List<student> aList = new List<student>(){
new student{id = 1,name = "s1"},
new student{id = 2,name = "s2"},
new student{id = 3,name = "s3"}
};
List<student> bList = new List<student>(){
new student{id = 2},
new student{id = 2},
new student{id = 3}
};
var cList = (from c in bList select c.id).Distinct();
foreach(var s in cList)
{
Console.WriteLine(s);
}
}
}
现在能去除bList 中的id重复项,
输出为
2
3
拉下来能不能用LINQ实现, 指定aList.id在cList取值得到集合为
2 S2
3 S3
------解决思路----------------------
cList.Select(x => aList.Where(y => y.id == x).FirstOrDefault());
------解决思路----------------------
alist.Where(i=>blist.Any(j=>j.Id==i.id))