-3

我有 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

4 回答 4

4

LINQ 方法返回IEnumerable<T>,并且可以对其进行操作IEnumerable<T>

你可以写

sequence.Where(...).Select(...)
于 2013-09-03T14:53:21.507 回答
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();
于 2013-09-03T15:11:17.710 回答
0

您可以使用Union.

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));
于 2013-09-03T14:58:35.250 回答
0

为什么你不能只是混合它们?list.where(.......).select(.......).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();
于 2013-09-03T14:54:50.217 回答