2017-07-20 13 views
0

これはpostですが、EntityFrameworkコアにはlinqクエリが実装されています。ここ は、C#の構文は次のとおりです。フィルタリングにはEntity Frameworkの項目が含まれています

DateTime today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); 
    var actionMenuGroup = Context.ActionMenuGroups 
        .Include(am => am.ActionMenus) 
        .Where(am => am.ActionMenus.Any(amm => amm.DeliveryDate.GetValueOrDefault(DateTime.MinValue).CompareTo(today) <= 0 && amm.AuthorizedCustomers.Any(ac => ac.CustomerId.Equals(CustomerId) && ac.DeliveryDate.GetValueOrDefault(DateTime.MinValue).CompareTo(today)<=0))) 
        .Select(g => new ActionMenuGroup() 
        { 
         Id = g.Id, 
         Name = g.Name, 
         SortOrder = g.SortOrder, 
         ActionMenus = g.ActionMenus.Where(amm => amm.DeliveryDate.GetValueOrDefault(DateTime.MinValue).CompareTo(today) <= 0 && amm.AuthorizedCustomers.Any(ac => ac.CustomerId.Equals(CustomerId) && ac.DeliveryDate.GetValueOrDefault(DateTime.MinValue).CompareTo(today) <= 0)).ToList() 
        }); 

、すべてが正常に動作します! しかし、ラムダフィルタ式を挿入した後、選択が間違っています。ここではラムダとの新しいバージョンは次のとおりです。

public Func<Model.ActionMenu, DateTime, int?, bool> filterActionMenu = (amm, oggi, CustomerId) => amm.DeliveryDate.GetValueOrDefault(DateTime.MinValue).CompareTo(oggi) <= 0 && amm.AuthorizedCustomers.Any(ac => ac.CustomerId.Equals(CustomerId) && ac.DeliveryDate.GetValueOrDefault(DateTime.MinValue).CompareTo(oggi) <= 0); 

    public IQueryable<ActionMenuGroup> GetActionMenuAndMenuGroupByCustomer(int? CustomerId) 
    { 
     DateTime today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); 
     var actionMenuGroup = Context.ActionMenuGroups 
        .Include(am => am.ActionMenus) 
        .Where(am => am.ActionMenus.Any(amm => filterActionMenu(amm,today, CustomerId))) 
        .Select(g => new ActionMenuGroup() 
        { 
         Id = g.Id, 
         Name = g.Name, 
         SortOrder = g.SortOrder, 
         ActionMenus = g.ActionMenus.Where(amm => filterActionMenu(amm, today, CustomerId)).ToList() 
        }); 
     return actionMenuGroup; 
    } 

は、LINQの構文にラムダを使用して上のenyoneのexprerienceていますか? おかげ

答えて

0

試してみてください。

public Expression<Func<Model.ActionMenu, DateTime, int?, bool>> filterActionMenu = (amm, oggi, CustomerId) => .... 
+0

ジェームズ、それは '' ICollectionをタイプのナビゲーションプロパティに式ツリー内で使用することができますどのように構文的に? –

関連する問題