这将是快速和肮脏的,但应该给你的想法。我以前做过,但找不到我做的地方,也不记得因为它已经这么久了。我需要搜索多个级别的列表....
示例:搜索“猪”
var query = context.Farms.OrderBy(x => x.FarmName).Select(f => new FarmItem
{
FarmId = f.FarmId,
FarmName = f.FarmName,
Fields = f.Fields.OrderBy(x => x.FieldName).Select(d => new FieldItem
{
FieldId = d.FieldId,
FieldName = d.FieldName,
Animals = d.Animals.OrderBy(x => x.AnimalName).Select(a => new AnimalItem
{
AnimalId = a.AnimalId,
AnimalName = a.AnimalName
})
})
});
我需要查看“pig”是否在FarmName,FieldName或AnimalName和
中答案 0 :(得分:0)
这应该有效(未经测试):
var query = context.Farms.OrderBy(x => x.FarmName).Select(f => new FarmItem
{
FarmId = f.FarmId,
FarmName = f.FarmName,
Fields = f.Fields.OrderBy(x => x.FieldName).Select(d => new FieldItem
{
FieldId = d.FieldId,
FieldName = d.FieldName,
Animals = d.Animals.OrderBy(x => x.AnimalName).Select(a => new AnimalItem
{
AnimalId = a.AnimalId,
AnimalName = a.AnimalName
}).Where(a => a.AnimalName.Contains("pig"))
}).Where(d => d.FieldName.Contains("pig") || d.Animals.Any())
}).Where(d => f.FarmName.Contains("pig") || f.Fields.Any());