将两个linq语句混合成一个?

时间:2013-09-03 14:50:57

标签: c# winforms linq

我有2个linq语句,它们都完全正常工作。我想知道是否可以将它们混合成一个并在一个linq之后获得正确的列表。

var result = list3.Where(Srodek => list4.Any(x => x == Srodek.Srodek.category1) &&
                                  (Srodek.Srodek.Source.Device == _text || Srodek.Srodek.ID.Device == _text))
                  .ToList();
var list666 = list3.Select(obj => new { obj, dt = DateTime.ParseExact(obj.LeftColumn, dateFormat, CultureInfo.InvariantCulture) })
                   .Where(x => x.dt >= czas11 && x.dt <= czas22)
                   .Select(x => x.obj).ToList();

4 个答案:

答案 0 :(得分:4)

LINQ方法返回IEnumerable<T>,可以在IEnumerable<T>上运行。

你可以写

sequence.Where(...).Select(...)

答案 1 :(得分:1)

一个清单:

var result = list3.Where(obj => {
                              var dt = DateTime.ParseExact(obj.LeftColumn, dateFormat, CultureInfo.InvariantCulture);
                              return (list4.Any(x => x == obj.Srodek.category1) &&
                              (obj.Srodek.Source.Device == _text || obj.Srodek.ID.Device == _text)) ||
                              (dt >= czas11 && dt <= czas22);})       
                  .ToList();

答案 2 :(得分:0)

为什么你不能混合它们? list.where(.........)选择(.......)toList();

var result = list3.Where(Srodek => list4.Any(x => x == Srodek.Srodek.category1) &&
                         (Srodek.Srodek.Source.Device == _text || Srodek.Srodek.ID.Device== _text))
                   .Select(obj => new { obj, dt = DateTime.ParseExact(obj.LeftColumn, dateFormat, CultureInfo.InvariantCulture) })
                  .Where(x => x.dt >= czas11 && x.dt <= czas22)
                  .Select(x => x.obj)
                  .ToList();

答案 3 :(得分:0)

您可以使用Union加入两个linq查询。

list3.Where(Srodek => list4.Any(x => x == Srodek.Srodek.category1) &&
                              (Srodek.Srodek.Source.Device == _text || Srodek.Srodek.ID.Device == _text))
               .Union(list3.Select(obj => new { obj, dt = DateTime.ParseExact(obj.LeftColumn, dateFormat, CultureInfo.InvariantCulture) })
               .Where(x => x.dt >= czas11 && x.dt <= czas22)
               .Select(x => x.obj));