在 .NET Core 查询里面加入 for

Join in .NET Core query inside for

我需要连接数据库中的2个表,从第二个中获取两个值。

但我必须在 for 语句中完成。

我该怎么做?

foreach (AvgValues avgValues in db.AvgValues
    .Where(avg => (firstDate == null) || (avg.Timestamp > DateTime.ParseExact(firstDate, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture)))
    .Where(avg => (secondDate == null) || (avg.Timestamp < DateTime.ParseExact(secondDate, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture))))
{
    ...
}

好的,我的解决方案是从 for 语句中取出查询:

var q =
    from a in db.AvgValues
    join c in db.Channels on a.Channel equals c.Channel 
    select new { AvgValues = a, Channels = c };

foreach (var x in q
    .Where(avg => (firstDate == null) || (avg.AvgValues.Timestamp > DateTime.ParseExact(firstDate, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture)))
    .Where(avg => (secondDate == null) || (avg.AvgValues.Timestamp < DateTime.ParseExact(secondDate, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture))))
    {
        ...
    }