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
        })
    })
});

我需要查看“猪”是否在 FarmName、FieldName 或 AnimalName 中,并且

  1. 如果在名称中找到“猪”,则返回农场,以及所有的田地,以及田地的动物
  2. 返回名称中带有“猪”的田地以及农场以及田地的所有动物
  3. 返回名称中带有“猪”的计划以及它的田地,它是田地的农场
4

1 回答 1

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());
于 2013-11-06T20:45:19.733 回答