Linq with entity Framework 使用函数到 select 列
Linq with entity Framework using a function to select columns
我正在尝试减少应用程序中的代码量。
我想转这个
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服务器,数据库不知道该函数做什么。我收到错误
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
我在几个地方使用了相同的 select 列表,因此它会阻止在添加字段时出现错误。
将您的字段声明为 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
};
我正在尝试减少应用程序中的代码量。
我想转这个
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服务器,数据库不知道该函数做什么。我收到错误
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
我在几个地方使用了相同的 select 列表,因此它会阻止在添加字段时出现错误。
将您的字段声明为 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
};