2016-12-12 5 views
0

에 따라 :LINQ 선택 나는 C# 및 LINQ에 대한 질문이 수집

을 나는 클래스를 항목 (부모) 여러 custom_fields (아이를) 할 수있다. custom_fields 클래스는 ID, 이름, 값의 3 가지 값을가집니다.

custom_field를 기준으로 필터링하고 싶습니다. 예를 들어 custom_field 색상의 값이 빨간색 인 모든 항목이 있습니다.

LINQ- 쿼리를 수행하는 방법이 약간 분실되었습니다. 나는 다음을 시도했다. 그러나 그것은 틀렸다.

items = items.Where(x => x.custom_fields.Where(y => y.Name == "color") 
             .Any(z => z.Value == "red") 
        ); 

올바른 쿼리는 어떻게 작성합니까?

미리 감사드립니다. 당신이 당신의 결과에 붉은 색으로 모든 항목을 얻고 싶다면

답변

6

.Where(x => x.custom_fields.Where(y => y.Name == "color").Any(z => z.Value == "red")); 

.Where(x => x.custom_fields.Any(y => y.Name == "color" && y.Value == "red")); 
+0

입니다 어떤 이유로 나를 0의 항목으로 남겨 둡니다. 쿼리하기 전에 많은 항목이 있습니다. 항목은 Enumerable이며 custom_fields ICollection입니다. 대부분의 항목에는 custom_fields (색상, 크기 등)가 여러 개 있습니다. 문제점을 일으킬 수있는 아이디어가 있습니까? – sebvst

+0

@sebvst 결과는'items' 또는'custom_fields'입니까? 데이터베이스에'Name == "color"'와'Value == "red"'가있는'custom_fields' 아이템이 있습니까? – fubo

+0

내 쿼리는'Name == "color"와'Value == "red"' – fubo

3

가정 항목해야하는 것은뿐만 아주 좋아 보인다 IEnumerable<T>

var redItems = 
    from item in items 
    from custom_field in items.custom_fields 
    where custom_field.Name == "color" && custom_field.Value == "red" 
    select item;