[c#]Linq 与实体框架使用函数来选择列

发布时间: 2017/2/28 20:00:54
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图减少我的应用程序中的代码量。

我想把这

 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);

到这

 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
        };

问题是它在将传递给 SQL Server 和数据库并不知道该怎么办的功能。我得到错误

在 LINQ 到实体不支持 LINQ 表达式节点类型 '调用'。

我在几个地方使用同一选择列表,因此它会停止添加 bug 匍匐当领域。

解决方法 1:

将你作为字段声明为 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
};
赞助商