给出以下代码,其中显示了包含300多个字段的模型中的字段列表:
Foo = await
(
_context.Foo
.Where(r => !StatusExceptionList.Contains(r.Status))
.Where(r => (Convert.ToDateTime(r.date) - today).TotalDays < 31)
.Where(r => r.Pid == PId)
)
.Union
(
_context.Foo
.Where(r => !DraftStatusExceptionList.Contains(r.Status))
.Where(r => r.Pid == PId)
.Where(r => r.Csstatus != "NA" || !String.IsNullOrEmpty(r.Csstatus))
.Where(r => !_context.Foo
.Where(rr => rr.Pid == PersonId)
.Select(rr => rr.Fooid)
.Contains(r.Fooid)
)
)
.ToListAsync();
是否可以将一个静态值为0的虚拟字段添加到union的一侧的记录中,将值1添加到union的另一侧的记录中,然后最后按虚拟字段排序内容?
这个想法是联盟的顶部先按顺序显示,然后在那之下,显示联合的第二部分。
以下示例是我在脑海中看到它并且它不起作用的方式,但是使用anon类型,但是使用*来从模型中抓取所有字段 -
Foo = await
(
_context.Foo
.Where(r => !StatusExceptionList.Contains(r.Status))
.Where(r => (Convert.ToDateTime(r.date) - today).TotalDays < 31)
.Where(r => r.Pid == PId)
.Select(rr => new
{
* //all fields that exist in r already need to be represented as well as the virtual field 'sortvalue'.....
SortValue = 0
});
)
.Union
(
_context.Foo
.Where(r => !DraftStatusExceptionList.Contains(r.Status))
.Where(r => r.Pid == PId)
.Where(r => r.Csstatus != "NA" || !String.IsNullOrEmpty(r.Csstatus))
.Where(r => !_context.Foo
.Where(rr => rr.Pid == PersonId)
.Select(rr => rr.Fooid)
.Contains(r.Fooid)
)
.Select(rrr => new
{
* //all fields that exist in r/rr already need to be represented as well as the virtual field 'sortvalue'.....
SortValue = 1
});
)
//...insert ordering logic here that uses sortValue to show the top query contents first, followed by the bottom queries contents
.OrderBy(r => sortValue);
.ToListAsync();
谢谢!
答案 0 :(得分:0)
这是使用northwind db的示例。匿名类型用于跟踪结果来自哪个联合部分。然后使用鉴别器o对结果进行排序,然后使用select将其转换回原始类型。
var users = db.Employees
.Where(emp => emp.FirstName == "Nancy")
.Select(emp => new { emp, o = 0 })
.Union( db.Employees
.Where(emp => emp.City == "Seattle")
.Select(u => new { emp, o = 1 })
)
.OrderBy(u => u.o)
.Select(u => u.emp);