【发布时间】:2018-03-30 22:09:55
【问题描述】:
假设我有六个LINQ queries。每个查询都包含上一个查询使用的运算符,并在链中再添加一个运算符:
Foo = await
(
_context.Foo
.Where(f => (Convert.ToDateTime(f.date1) - today).TotalDays < 31)
)
.ToListAsync();
Foo = await
(
_context.Foo
.Where(f => (Convert.ToDateTime(f.date1) - today).TotalDays < 31)
.Where(f => f.recordid == x)
)
.ToListAsync();
Foo = await
(
_context.Foo
.Where(f => (Convert.ToDateTime(f.date1) - today).TotalDays < 31)
.Where(f => f.recordid == x)
.Where(f => !StatusExceptionList.Contains(r.Status))
)
.ToListAsync();
Foo = await
(
_context.Foo
.Where(f => (Convert.ToDateTime(f.date1) - today).TotalDays < 31)
.Where(f => f.recordid == x)
.Where(f => !StatusExceptionList.Contains(r.Status))
.OrderBy(f => f.date1)
)
.ToListAsync();
Foo = await
(
_context.Foo
.Where(f => (Convert.ToDateTime(f.date1) - today).TotalDays < 31)
.Where(f => f.recordid == x)
.Where(f => !StatusExceptionList.Contains(r.Status))
.OrderBy(f => f.date1)
.ThenBy(f => f.date2)
)
.ToListAsync();
Foo = await
(
_context.Foo
.Where(f => (Convert.ToDateTime(f.date1) - today).TotalDays < 31)
.Where(f => f.recordid == x)
.Where(f => !StatusExceptionList.Contains(r.Status))
.OrderBy(f => f.date1)
.ThenBy(f => f.date2)
.ThenBy(f => f.recordid)
)
.ToListAsync();
我想将其压缩为一个查询,并在不同的 linq 运算符之间编织逻辑,正如此伪代码所示,但这根本不起作用。我用谷歌搜索了一下,不知道下一步该尝试什么:
Foo = await
(
_context.Foo
.Where(f => (Convert.ToDateTime(f.date1) - today).TotalDays < 31)
if (some logic)
{
.Where(f => f.recordid == x)
}
if (some different logic)
{
.Where(f => !StatusExceptionList.Contains(r.Status))
}
if (some different logic)
{
.OrderBy(f => f.date1)
}
if (some different logic)
{
.ThenBy(f => f.date2)
}
if (some different logic)
{
.ThenBy(f => f.recordid)
}
)
.ToListAsync();
谁能指出我正确的方向或告诉我我是否试图以错误的方式解决这个问题?非常感谢!!!
【问题讨论】:
标签: c# linq asp.net-core