2009-05-25 1 views
1

В этом запросе всегда нужен элемент типа «normal».
Если флаг _includeX установлен, я также хочу использовать элементы типа «рабочая область».
Есть ли способ написать это как один запрос? Или создать предложение where на основе _includeX перед отправкой запроса?Построение предложения 'where' в запросе Linq

if (_includeX) { 
    query = from xElem in doc.Descendants(_xString) 
     let typeAttributeValue = xElem.Attribute(_typeAttributeName).Value 
     where typeAttributeValue == _sWorkspace || 
       typeAttributeValue == _sNormal 
     select new xmlThing 
     { 
      _location = xElem.Attribute(_nameAttributeName).Value, 
      _type = xElem.Attribute(_typeAttributeName).Value, 
     }; 
} 
else { 
    query = from xElem in doc.Descendants(_xString) 
     where xElem.Attribute(_typeAttributeName).Value == _sNormal 
     select new xmlThing 
     { 
      _location = xElem.Attribute(_nameAttributeName).Value, 
      _type = xElem.Attribute(_typeAttributeName).Value, 
     }; 
} 

ответ

1

Вы можете разбить его на отдельный предикат:

Predicate<string> selector = x=> _includeX 
    ? x == _sWorkspace || x == _sNormal 
    : x == _sNormal; 

query = from xElem in doc.Descendants(_xString) 
     where selector(xElem.Attribute(_typeAttributeName).Value) 
     select new xmlThing 
     { 
      _location = xElem.Attribute(_nameAttributeName).Value, 
      _type = xElem.Attribute(_typeAttributeName).Value, 
     }; 

или инлайн условие:

query = from xElem in doc.Descendants(_xString) 
    let typeAttributeValue = xElem.Attribute(_typeAttributeName).Value 
    where (typeAttributeValue == _sWorkspace && _includeX) || 
      typeAttributeValue == _sNormal 
    select new xmlThing 
    { 
     _location = xElem.Attribute(_nameAttributeName).Value, 
     _type = xElem.Attribute(_typeAttributeName).Value, 
    }; 

Или удалить использование выражения запроса и сделать это таким образом: -

var all = doc.Descendants(_xString); 
var query = all.Where(xElem=> { 
     var typeAttributeValue = xElem.Attribute(_typeAttributeName).Value; 
     return typeAttributeValue == _sWorkspace && includeX) || typeAttributeValue == _sNormal; 
}) 
.Select(xElem => 
    select new xmlThing 
    { 
     _location = xElem.Attribute(_nameAttributeName).Value, 
     _type = xElem.Attribute(_typeAttributeName).Value, 
    }) 

Или объедините первый и thi rd и do:

Predicate<string> selector = x=> _includeX 
    ? x == _sWorkspace || x == _sNormal 
    : x == _sNormal; 

query = doc.Descendants(_xString) 
     .Where(xElem => selector(xElem.Attribute(_typeAttributeName).Value)) 
     .Select(xElem => new xmlThing 
     { 
      _location = xElem.Attribute(_nameAttributeName).Value, 
      _type = xElem.Attribute(_typeAttributeName).Value, 
     };) 

Все зависит от того, что будет работать в чистоте в вашем контексте.

Сделайте себе одолжение и купите (и прочитайте!) C# в глубину, и все это будет иметь смысл намного быстрее, чем изучение этого материала по частям ...