我想找到 HasAccess 列等于具有相同 ID 的对象的 HasAccess 属性的 EntityPermission。
所以你有一张桌子EntityPermissions
。此表中的每个EntityPermission
都至少有一个布尔属性HasAccess
和一个长属性的主键Id
此外,您有一个对象列表,其中每个对象至少有一个 CompareId 和一个 HasAccess。
如果我正确阅读了您的要求,您希望所有具有 Id 的 EntityPermissions 也是列表中的 CompareId,并且具有相同的 HasAccess 值。
因此,如果您的列表具有值:
{10, false}, {11, true}, {12, false},
你有 EntityPermissiont:
Id HasAccess
09 true don't want this one, Id is not in the list
10 true don't want this one, Id is in the list, but HasAccess incorrect
11 true I want this one: Id is in the list, HasAccess is correct
12 false I want this one: Id is in the list, HasAccess is correct
通常你会用Where(x => y.Contains(x))
这个。问题在于,您只能选择一个属性。
var checkValues = new
{
new {CompareId = 10, HasAccess = false},
new {CompareId = 11, HasAccess = true},
new {CompareId = 12, HasAccess = false},
}
var result = dbContext.EntityPermissions.Select(entityPermission => new
{
ValueToCompare = new
{
CompareId = entityPermission.Id,
HasAccess = entityPermission.HasAccess,
},
Original = entityPermission,
})
// keep only those selected items that have a ValueToCompare in CheckValues
.Where(selectedItem => checkValues.Contains(selectedItem.ValueToCompare)
// from the remaining items, extract the original EntityPermission
.Select(selectedItem => selectedItem.Original);