[英]Linq to Entities 4.0 ('Include'): In search of an elegant way
I'm learning Linq to entities on my own and I have a bit of a question. 我正在向实体学习Linq,我有一个问题。 Take a look at the following method.
看一下下面的方法。 It returns the latest completed task for every item inside the
List of WorkflowsInProgress
. 它
List of WorkflowsInProgress
每个项目返回最新完成的任务。 This method is being called within an other method where i'm using the fields of TasksInProgress
, but i'm also using properties of the linked Tasks and WorkflowInProgress
. 我正在使用
fields of TasksInProgress
的其他方法中调用此方法,但是我还使用the linked Tasks and WorkflowInProgress
属性。 As you will see I've added the Include
to the query. 如您所见,我已将
Include
到查询中。
public List<TasksInProgress> GetLatestTaskForWorkflowsInProcess(List<WorkflowInProgress> pWorkflowsInProcess)
{
List<TasksInProgress> tasksLst = new List<TasksInProgress>();
List<Int64> workflowIds = pWorkflowsInProcess.Select(w => w.Id).ToList();
using (TaskWorkflowEntities myDatacontext = new TaskWorkflowEntities())
{
tasksLst = (from taskP in myDatacontext.TasksInProgress.Include("Tasks").Include("WorkflowInProgress")
where (taskP.RunningState == (int)WorkflowRunningStates.Completed) &&
workflowIds.Contains(taskP.WorkflowInProgressId) &&
taskP.Completed == (from sTaskP in myDatacontext.TasksInProgress
where sTaskP.WorkflowInProgressId == taskP.WorkflowInProgressId
group sTaskP by sTaskP.WorkflowInProgressId into gSTaskP
select gSTaskP.Max(g => g.Completed)).FirstOrDefault()
select taskP).ToList<TasksInProgress>();
}
return tasksLst;
}
My Question is: 我的问题是:
'Is there a more elegant way to include other tables inside a Query?'
“是否有更优雅的方式在查询中包含其他表?” Because i don't like those hardcoded objectnames just sitting there' (Imagine if the tablename changes...)
因为我不喜欢只是坐在那里的那些硬编码对象名(想象一下,如果表名更改了...)
Or is there any other way that i can use to include the fields of linked objects/navigational properties?
还是有其他方法可以用来包含链接对象/导航属性的字段?
Note: Example of the methode above this one: 注意:以上方法的示例:
foreach(TasksInProgress taskInProc in _taskWorkflowS.GetLatestTaskForWorkflowsInProcess(currentWorkflowsInProcess))
{
//Do something with (int)taskInProc.Tasks.TaskOrder
//Do something with taskInProc.WorkflowInProgress.WorkflowId
// ...
//for Instance
int i = 0;
i = _taskWorkflowS.GetAmountOfTasksForWorkflow(taskInProc.WorkflowInProgress.WorkflowId, (int)taskInProc.Tasks.TaskOrder)
if (i > 0 )
{ ... }
}
Update: using lambda expression as parameter of the Include
doesn't appear to be working due to the fact that it only excepts string (see image below): 更新:使用lambda表达式作为
Include
参数似乎不起作用,原因是它仅排除字符串(参见下图):
Edit: Answer before the question was changed to Entity Framework 4.0. 编辑:将问题更改为Entity Framework 4.0之前的答案。 This will only work for EF 4.1 and later
这仅适用于EF 4.1和更高版本
You can have 你可以有
.Include(o => o.Tasks)
if you add 如果您添加
using System.Data.Entity;
at least you are not using strings then and you will get errors if the table changes 至少您当时没有使用字符串,并且如果表发生更改,您将得到错误
You can write an extension method that use a lambda expression instead of a string: 您可以编写使用lambda表达式而不是字符串的扩展方法:
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> query, Expression<Func<T, object>> selector)
{
MemberExpression body = selector.Body as MemberExpression;
return query.Include(body.Member.Name);
}
and use it like: 并像这样使用它:
myDatacontext.TasksInProgress.Include(q=>q.Tasks)
.Include(q=>q.WorkflowInProgress)
I have not tested it, but it should work. 我没有测试过,但是应该可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.