1

所以我正在使用 LINQ 和这个(经过清理的)代码进行数据库查询:

var dateList =
            Table1.Where(t1 => t1.column1.Contains(stringUsed)) //stringUsed is a parameter of the method containing this code
                .Join(Table2, t1 => t1.columnName2, t2 => t2.columnName2,
                    (t1, t2) => new {t1, t2})
                .Join(Table3, t1t2 => t1t2.t1.column3, t3 => t3.column3,
                    (t1t2, t3) => new {Date = t3.column4})
                .Select(d => d.Date.Value) //Dates are stored as a DateTime? and I need to convert it to DateTime here
                .Distinct()
                .ToList();

最终结果是一个包含 2000 多个唯一日期的列表(一切都很好)。根据用户要求,我只需要收集查询将创建的最新 90 条记录的日期(数据已在数据库中排序,因此无需在此处执行此操作)。

当我尝试在and.Take(90)之间插入时,我的问题就出现了。结果是一个只有 13 条记录的列表。我在这里和其他地方搜索,看看是否有其他人遇到过这个问题,但找不到任何东西。有任何想法吗?.Distinct().ToList()

4

1 回答 1

1

向@usr 提供帮助以帮助解决此问题。这是根据需要提取 90 条记录的代码:

var dateList =
            Table1.Where(t1 => t1.column1.Contains(stringUsed)) //stringUsed is a parameter of the method containing this code
                .Join(Table2, t1 => t1.columnName2, t2 => t2.columnName2,
                    (t1, t2) => new {t1, t2})
                .Join(Table3, t1t2 => t1t2.t1.column3, t3 => t3.column3,
                    (t1t2, t3) => new {Date = t3.column4})
                .Select(d => d.Date.Value) //Dates are stored as a DateTime? and I need to convert it to DateTime here
                .Distinct()
                .OrderByDescending(d => d)
                .Take(90)
                .ToList();

显然添加.OrderBy()是需要做的。感谢大家。

于 2016-06-24T21:07:32.833 回答