我有 2 节课
public class ClassA
{
public int Id { get; set; }
public string Name { get; set; }
}
和
public class ClassB
{
public int Id { get; set; }
public string Name { get; set; }
public ClassA ClassA { get; set; }
}
我正在尝试使用 A 类列表过滤 B 类列表
void Main()
{
var ListA = new List<ClassA>();
var a1 = new ClassA() {Id=1, Name = "A1"};
var a2 = new ClassA() {Id=2, Name = "A2"};
var a3 = new ClassA() {Id=3, Name = "A3"};
ListA.Add(a1);
ListA.Add(a2);
ListA.Add(a3);
var FilterListA = new List<ClassA>();
FilterListA.Add(a1);
FilterListA.Add(a2);
var ListB = new List<ClassB>();
var b1 = new ClassB() {Id=1, Name="B1" ,ClassA= a1};
var b2 = new ClassB() {Id=1, Name="B1", ClassA= a2};
var b3 = new ClassB() {Id=1, Name="B1", ClassA= a3};
var b4 = new ClassB() {Id=1, Name="B1", ClassA= a3};
ListB.Add(b1);
ListB.Add(b2);
ListB.Add(b3);
ListB.Add(b4);
如果我使用它会起作用
var query = from b in ListB
join a in FilterListA
on b.ClassA equals a
select new { Name = b.Name, ClassAName = a.Name };
Console.WriteLine(query.ToList());
但我想做这样的事情......但我不知道怎么做
Console.WriteLine(ListB.Where(o => o.ClassA IsIncluded In FilterListA));
}
我尝试使用包含但没有成功。谢谢