2009-11-30 2 views
4

상자 목록이 있고 여러 항목을 가질 수있는 상자가 있다고 가정합니다. 내가 지정된 모든 항목을 포함하는 모든 상자를 반환 할 수 있습니다 엔티티 쿼리에 LINQ를 구축하기 위해 노력하고있어 박스 (ID) LINQ-to-Entity를 사용하여 포함 된 개체로 쿼리하는 방법

  • 항목 (아이디, boxId)
    • . 그것은 상자의 구현에 따라 도움

    답변

    0

    내가 JaredPar 기여에 감사를 발견 한 것입니다 회계 있는지 확인하기 위해 교차 확장 방법을 사용할 수 있습니다.

    List<Location> FindLocationContainingAllItems(List<int> itemIds) 
    { 
        var itemQuery = from item in ctx.Items 
            select item; 
    
        // Workaround the Where In Clause (http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0) 
        itemQuery = itemQuery.Where(BuildContainExpression<Items, int>(i=> i.Id, itemIds)); 
    
        int itemCount = itemIds.Count(); 
    
        var locQuery = from loc in ctx.Locations 
            from box in loc.Boxes 
            where (from items in box.Items select items).Intersect(itemQuery).Count == itemCount 
            select loc; 
    
        return locQuery.ToList(); 
    
    } 
    
    +0

    이 솔루션에 대해 어떻게 생각하는지 알고 싶습니다. – pdiddy

    4

    에 대한

    List<Box> FindBoxContainingAllSpecifiedItems(List<int> itemIds) 
    { 
        var q = from box in ctx.Boxes 
          where ??? 
    } 
    

    감사합니다. 하지만 잠시 동안은 IEnumerable<int> 유형의 항목이있는 항목이 있다고 말합니다. 이 경우 당신은 항목이 모두 여기에

    var q = from box in ctx.Boxes 
         where box.Items.Intersect(itemIds).Count() == itemIds.Count; 
    
    +0

    덕분에 아 내가 – pdiddy

    +0

    을이을하려고합니다 내가 IEnumerable을 THIE이없는 경우 하지만 IEnumerable을 ...? – pdiddy

    +0

    @pdiddy, 항목 API는 무엇입니까? – JaredPar

    관련 문제