简体   繁体   English

Linq与实体Framework使用功能选择列

[英]Linq with entity Framework using a function to select columns

I'm trying to reduce the amount of code in my application. 我正在尝试减少应用程序中的代码量。

I want to turn this 我想把这个

 return _db.SnapshotQueues
               .Include(c => c.SnapshotDefinition)
               .Include(c => c.SnapshotDefinition.Client)
               .Include(c => c.Server)
               .Select(s => new SnapshotQueueModel()
               {
                   SnapshotQueueID = s.SnapshotQueueID,
                   SnapshotDefinitionID = s.SnapshotDefinitionID,
                   ScheduleID = s.ScheduleID,
                   DateCreated = s.DateCreated,
                   Protected = s.Protected,
                   StartTime = s.StartTime,
                   FinishTime = s.FinishTime,
                   Processed = s.Processed,
                   CoreSnapshotDatabaseName = s.CoreSnapshotDatabaseName,
                   Removed = s.Removed,
                   SnapshotID = s.SnapshotID,
                   EmailNotification = s.EmailNotification,
                   Email = s.Email,
                   ServerID = s.ServerID,
                   DateRequested = s.DateRequested,
                   Canceled = s.Canceled,
                   Active = s.Active,
                   LastSnapshotQueueActionID = s.SnapshotQueueActions.OrderByDescending(o => o.ActionDate).ThenByDescending(o => o.SnapshotQueueActionID).FirstOrDefault().ActionID,
                   LastAction = s.SnapshotQueueActions.OrderByDescending(o => o.ActionDate).ThenByDescending(o => o.SnapshotQueueActionID).FirstOrDefault().SnapshotAction.Description,
                   SnapshotLogCount = s.SnapshotGenerationLogs.Count(),

                   SnapshotDefinition = s.SnapshotDefinition.Name,
                   Server = s.Server.ServerName,
                   ScheduleName = s.Schedule.Name,
                   ClientName = s.SnapshotDefinition.Client.ClientName_Long
               }
               )
               .Where(s => s.SnapshotDefinitionID == snapshotDefinitionID)
               .OrderBy(sortFieldExpression);

into this 进入这个

 return _db.SnapshotQueues
               .Include(c => c.SnapshotDefinition)
               .Include(c => c.SnapshotDefinition.Client)
               .Include(c => c.Server)
               .Select(s => _snapshotQueueModelGet(s))
               .Where(s => s.SnapshotDefinitionID == snapshotDefinitionID)
               .OrderBy(sortFieldExpression);

        private readonly Func<SnapshotQueue, SnapshotQueueModel> _snapshotQueueModelGet = s => new SnapshotQueueModel
        {
            SnapshotQueueID = s.SnapshotQueueID,
            SnapshotDefinitionID = s.SnapshotDefinitionID,
            ScheduleID = s.ScheduleID,
            DateCreated = s.DateCreated,
            Protected = s.Protected,
            StartTime = s.StartTime,
            FinishTime = s.FinishTime,
            Processed = s.Processed,
            CoreSnapshotDatabaseName = s.CoreSnapshotDatabaseName,
            Removed = s.Removed,
            SnapshotID = s.SnapshotID,
            EmailNotification = s.EmailNotification,
            Email = s.Email,
            ServerID = s.ServerID,
            DateRequested = s.DateRequested,
            Canceled = s.Canceled,
            Active = s.Active,
            LastSnapshotQueueActionID =
                s.SnapshotQueueActions.OrderByDescending(o => o.ActionDate)
                    .ThenByDescending(o => o.SnapshotQueueActionID)
                    .FirstOrDefault()
                    .ActionID,
            LastAction =
                s.SnapshotQueueActions.OrderByDescending(o => o.ActionDate)
                    .ThenByDescending(o => o.SnapshotQueueActionID)
                    .FirstOrDefault()
                    .SnapshotAction.Description,
            SnapshotLogCount = s.SnapshotGenerationLogs.Count(),
            SnapshotDefinition = s.SnapshotDefinition.Name,
            Server = s.Server.ServerName,
            ScheduleName = s.Schedule.Name,
            ClientName = s.SnapshotDefinition.Client.ClientName_Long
        };

The problem is that it passes in to SQL Server, and the database doesn't know what to do with the function. 问题是它传递给SQL Server,而数据库不知道该函数如何处理。 I get the error 我得到错误

The LINQ expression node type 'Invoke' is not supported in LINQ to Entities. LINQ to Entities不支持LINQ表达式节点类型'Invoke'。

I'm using the same select list in a few places, so it would stop bugs creeping in when field are added. 我在几个地方都使用了相同的选择列表,因此当添加字段时,它将阻止臭虫蔓延。

Declare your field as Expression<Func<SnapshotQueue, SnapshotQueueModel>> : 将您的字段声明为Expression<Func<SnapshotQueue, SnapshotQueueModel>>

private readonly Expression<Func<SnapshotQueue, SnapshotQueueModel>> _snapshotQueueModelGet = s => new SnapshotQueueModel
{
    SnapshotQueueID = s.SnapshotQueueID,
    SnapshotDefinitionID = s.SnapshotDefinitionID,
    ScheduleID = s.ScheduleID,
    DateCreated = s.DateCreated,
    Protected = s.Protected,
    StartTime = s.StartTime,
    FinishTime = s.FinishTime,
    Processed = s.Processed,
    CoreSnapshotDatabaseName = s.CoreSnapshotDatabaseName,
    Removed = s.Removed,
    SnapshotID = s.SnapshotID,
    EmailNotification = s.EmailNotification,
    Email = s.Email,
    ServerID = s.ServerID,
    DateRequested = s.DateRequested,
    Canceled = s.Canceled,
    Active = s.Active,
    LastSnapshotQueueActionID =
        s.SnapshotQueueActions.OrderByDescending(o => o.ActionDate)
            .ThenByDescending(o => o.SnapshotQueueActionID)
            .FirstOrDefault()
            .ActionID,
    LastAction =
        s.SnapshotQueueActions.OrderByDescending(o => o.ActionDate)
            .ThenByDescending(o => o.SnapshotQueueActionID)
            .FirstOrDefault()
            .SnapshotAction.Description,
    SnapshotLogCount = s.SnapshotGenerationLogs.Count(),
    SnapshotDefinition = s.SnapshotDefinition.Name,
    Server = s.Server.ServerName,
    ScheduleName = s.Schedule.Name,
    ClientName = s.SnapshotDefinition.Client.ClientName_Long
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM