这是对此处找到的相关主题的跟进 https://stackoverflow.com/questions/1987485/conditionally-assign-c-var-as-elegant-as-it-get s
如果我正在执行以下操作:
var query = (SearchString == "" ?
(
from MEDIA in xdoc.Descendants("MEDIA")
select new
{
PLAY = MEDIA.Element("PLAY").Value,
PIC = MEDIA.Element("PIC").Value,
TTL = MEDIA.Element("TTL").Value
}
):
from MEDIA in xdoc.Descendants("MEDIA")
where MEDIA.Element("TTL").ToString().ToLower().Contains(SearchString)
select new
{
PLAY = MEDIA.Element("PLAY").Value,
PIC = MEDIA.Element("PIC").Value,
TTL = MEDIA.Element("TTL").Value
}
) ;
我将如何声明查询类型以使其在类级别成为静态?
或者,在引用的帖子中,Marc Gravell 指出了一种不同的方法
IQueryable<Part> query = db.Participant;
if(email != null) query = query.Where(p => p.EmailAddress == email);
if(seqNr != null) query = query.Where(p => p.SequenceNumber == seqNr);
...
在我的情况下,我将如何声明/重新编码查询?这是我的疯狂尝试:)
IEnumerable<XElement> query = xdoc.Descendants("MEDIA");
if (SearchString != "" )
query = query.Where(m => m.Element("TTL").ToString().ToLower().Contains(SearchString));
谢谢你。