更新时间:2023-11-29 16:09:10
有很多干净的方式来处理这个问题。如果你想第一个记录
对应 ID
你可以说:
There are a lot of clean ways to handle this. If you want the first Record
corresponding to id
you can say:
Record record = data.Records.FirstOrDefault(r => r.Id == id);
if(record != null) {
// record exists
}
else {
// record does not exist
}
如果您只想知道如果这样的记录
存在:
If you only want to know if such a Record
exists:
return data.Records.Any(r => r.Id == id); // true if exists
如果你想的数有多少这样的记录
存在:
If you want a count of how many such Record
exists:
return data.Records.Count(r => r.Id == id);
如果你想要一个枚举(的IEnumerable<记录>
)的所有这样的记录
:
If you want an enumeration (IEnumerable<Record>
) of all such Record
:
return data.Records.Where(r => r.Id == id);