1

我有一个按关键字和区域过滤“检查员”的功能。“Inspectors”和“Areas”之间的关系是多对多的。

public IQueryable<Inspector> Search(string keywords, int? areaId)
{
    var qry = this.GetAllInspectors();

    if (!String.IsNullOrEmpty(keywords))
       qry = qry.Search(keywords);

    if (areaId.HasValue)
       qry = qry.ByAreaId(areaId.Value);

    var search = from a in _entities.Areas
                 select new InspectorSearchResult
                 {
                     Area = i.Areas,
                     Inspectors = (.....).ToList()
                  };  // ??

    return qry.OrderBy(i => i.LastName).ThenBy(i => i.FirstName);
}

InspectorSearchResult.cs

public class InspectorSearchResult
{
    public Area Area { get; set; }
    public List<Inspector> Inspectors { get; set; }
}

如何修改该查询仅以嵌套形式获取“按区域检查员”?

更新:

试过这个但得到:“......方法不能被翻译成商店表达式。”

public List<InspectorSearchResult> Search(string keywords, int? areaId)
{
       var qry = this.GetAllInspectors();

       if (!String.IsNullOrEmpty(keywords))
          qry = qry.Search(keywords);

       if (areaId.HasValue)
          qry = qry.ByAreaId(areaId.Value);

       var search = from i in qry
                    from a in i.Areas
                    orderby a.AreaName
                    select new InspectorSearchResult
                     {
                         Area = a,
                         Inspectors = (from ins in a.Inspectors
                                       orderby ins.LastName, ins.FirstName
                                       select ins).ToList()
                     };

        return search.ToList();
 } 
4

1 回答 1

1

这是你想要达到的目标吗?

public List<InspectorSearchResult> Search(string keywords, int? areaId)
{
    var qry = this.GetAllInspectors();

    if (!String.IsNullOrEmpty(keywords))
        qry = qry.Search(keywords);

    if (areaId.HasValue)
        qry = qry.ByAreaId(areaId.Value);

    var search = from i in qry
        from a in i.Areas
        orderby a.AreaName
        select new InspectorSearchResult
        {
            Area = a
        };

    foreach(var x in search)
    {
        x.Inspectors = (
            from ins in a.Inspectors
            orderby ins.LastName, ins.FirstName
            select ins)
            .ToList();
    }

    return search.ToList();
}
于 2013-03-14T15:44:28.013 回答