C# 在linq查询中动态选择字段

C# 在linq查询中动态选择字段,c#,linq,C#,Linq,我有一个linq查询,我想动态地只选择用户请求的字段 目前,我正在将我的作业映射到数据转换对象,如下所示: var jobs = (from p in jobsDB select new JobReportDTO() { JobID = p.JobID, EventType = p.EventType, DateApproved = p.ApprovedDate, DateEntered = p.EnteredDate, DateClosed = p.Cl

我有一个linq查询,我想动态地只选择用户请求的字段

目前,我正在将我的作业映射到数据转换对象,如下所示:

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
在该函数中,我有许多布尔变量,用于确定是否应将该字段发送到视图,即:

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查询以仅显示选定字段

我试过了

var jobs = (from p in jobsDB
    select new JobReportDTO()
    {
        JobID = p.JobID,
        jobReport.ShowEventType == true ? EventType = p.EventType : "",
        ... etc
但是我得到了“无效的初始化器成员声明器”

请尝试以下方法:

EventType = jobReport.ShowEventType == true ? p.EventType : string.Empty,

如果您能负担得起LINQ方法语法,并对诸如
public bool Show{DTOPropertyName}{get;set;}
之类的选项使用强命名约定,那么借助
System.LINQ.Expressions
和下面的小助手方法,您的生活就会轻松得多

public static class MyExtensions
{
    public static IQueryable<TResult> Select<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector, object options)
    {
        var memberInit = (MemberInitExpression)selector.Body;
        var bindings = new List<MemberBinding>();
        foreach (var binding in memberInit.Bindings)
        {
            var option = options.GetType().GetProperty("Show" + binding.Member.Name);
            if (option == null || (bool)option.GetValue(options)) bindings.Add(binding);
        }
        var newSelector = Expression.Lambda<Func<TSource, TResult>>(
            Expression.MemberInit(memberInit.NewExpression, bindings), selector.Parameters);
        return source.Select(newSelector);
    }
}

如果在调试器中设置断点并检查
newSelector
变量,您将看到只有没有
ShowProperty
的属性(如
JobID
)或者包含
ShowProperty=true

据我所知,当相应的
bool
被删除时,他不希望
EventType
成为其对象的一部分false@AlexanderDerckJobReportDTO已经是一个属性/字段。他可以用其他方式将其设置为null,我知道,但是“我如何修改我的linq查询以仅显示选定字段?”听起来好像他想要一个只有特定属性的对象。这很完美,谢谢,我可以使用null对象(而不是完全缺少属性)这只会从数据库中获取所需的记录,对吗?它只是一个
Select
部分,控制获取的字段(列)。关于记录,如果您有,它将获取您在
Where
子句中指定的内容。yeh抱歉,我指的是列:)没问题,那么是:)假设使用EF,您可以添加
var sql=jobs.ToString()
并查看实际的SQL查询。
var jobs = jobsDB.Select(p => 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 
}, jobReport);