我有 2 个实体A
,B
每个实体都有一个字段Number
,我需要从不存在的实体A
中获取所有行。Number
B
有任何想法吗?需要加盟吗?
这会奏效吗?
var result = entityA.Where(a => !entityB.Any(b => a.Number == b.Number))
非常粗糙,但你可以在没有这样的连接的情况下做到这一点
var listB = b.Select(o => o.Number).Distinct();
var result = a.Where(o => !listB.Contains(o.Number));
由于您没有提供太多信息,因此很难说。但也许你可以做这样的事情:
public class TestEntity
{
public int Number { get; set; }
public TestEntity(int num)
{
Number = num;
}
}
public void WriteResult()
{
List<TestEntity> lstA = new List<TestEntity>();
List<TestEntity> lstB = new List<TestEntity>();
for (int i = 0; i < 10; i++)
{
lstA.Add(new TestEntity(i));
lstB.Add(new TestEntity(i+4));
}
List<TestEntity> result = lstA.FindAll(teA => !lstB.Any(teB => teA.Number == teB.Number)); //This will give you all entities in lstA that does not have any equals in lstB
foreach (var item in result)
{
Console.WriteLine(item.Number);
}
}