动态选择linq查询中的字段 [英] Dynamically selecting fields in a linq query
本文介绍了动态选择linq查询中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个linq查询,我想只动态选择用户请求的字段.
I have a linq query where I’d like to dynamically select only the fields requested by my user.
当前,我正在将我的Jobs映射到这样的数据转换对象:
Currently I’m mapping my Jobs to a data transformation object like this:
var jobs = (from p in jobsDB
select new JobReportDTO()
{
JobID = p.JobID,
EventType = p.EventType,
DateApproved = p.ApprovedDate,
DateEntered = p.EnteredDate,
DateClosed = p.ClosedDate,
StartDate = p.StartDate,
FinishDate = p.FinishDate,
InsuredName = p.InsuredName,
StreetAddress = p.StreetAddress,
Suburb = p.Suburb,
State = p.State,
Postcode = p.Postcode,
.... etc
在此函数中,我有许多布尔变量,用于标识是否应将该字段发送到视图,即:
Within this function I have a number of boolean variables that identify whether that field should be sent to the view, i.e.:
public bool ShowInsuredName { get; set; }
public bool ShowSuburb { get; set; }
public bool ShowICLA { get; set; }
public bool ShowClaimNumber { get; set; }
public bool ShowFileMananger { get; set; }
public bool ShowSupervisor { get; set; }
public bool ShowStatus { get; set; }
... etc
如何修改linq查询以仅显示所选字段?
How can I modify my linq query to show selected fields only?
我尝试过
var jobs = (from p in jobsDB
select new JobReportDTO()
{
JobID = p.JobID,
jobReport.ShowEventType == true ? EventType = p.EventType : "",
... etc
但是正在获取无效的初始化器成员声明器"
But am getting "invalid initialiser member declarator"
推荐答案
尝试这种方式:
EventType = jobReport.ShowEventType == true ? p.EventType : string.Empty,
这篇关于动态选择linq查询中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文