0

我首先使用 EF 代码。现在,我很难弄清楚如何编写 LINQ 以将数据检索到我的 Controller 中的模型中,以便在视图中显示它们。基本上,我正在接收 HolterTest 数据的提要,并且我正在尝试为执行一系列特定任务以处理 HolterTest 的人员创建一个工作列表,允许他们在任务完成时对其进行标记,并提供位置状态各个测试都在流程中 基本的 Task 类是这样他们可以添加或更改流程中的步骤,displayOrder 是任务完成的顺序。WorkTask 是任务的特定实例,允许我们标记完成它的人员和时间。WorkItem 是一种复杂类型,包括 HolterTest、WorkTasks 列表和状态信息,包括 WorkTasks 何时完成。

模型类:

public class HolterTest
{
    public Int32 HolterTestID { get; set; }
    public string PatientNumber { get; set; }
    public string LastName { get; set; }
    public DateTime RecordingStartDateTime { get; set; }
    public System.Nullable<DateTime> AppointmentDateTime { get; set; }

}

public class Task
{
    public Int32 TaskID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Int32 DisplayOrder { get; set; }
    public bool IsActive { get; set; }
}

public class WorkTask
{
    public Int32 WorkTaskID { get; set; }
    public Task Task { get; set; }
    public bool IsCompleted { get; set; }
    public System.Nullable<DateTime> CompletedDateTime { get; set; }
    public string CompletedBy { get; set; }
}   

public class WorkItem
{
    public Int32 WorkItemID { get; set; }
    public HolterTest HolterTest { get; set; }
    public string Status { get; set; }
    public List<WorkTask> WorkTasks { get; set; }
    public bool IsStarted { get; set; }
    public bool IsCompleted { get; set; }
    public System.Nullable<DateTime> CompletedDateTime { get; set; }

}

目前我有一个业务逻辑函数,它获取 HolterTests 列表,找到没有 WorkItem 的那些,并创建 WorkItems,根据当前活动的任务列表关联包括 WorkTasks 在内的 HolterTests。

我的问题是如何编写 LINQ 来为我的 WorkItemController 获取所有 WorkItems(及其子项),以便我可以通过 PatientNumber 在 View(WorkItems where IsCompleted = false)中显示要执行的工作,并使其可以更新特定 WorkItem 的 WorkTasks。

4

2 回答 2

1

You want to access the related using it's navigation properties. Note that in your example, you haven't setup the navigation properties to be virtual. You should should update your model like this:

public class WorkItem
{
    public Int32 WorkItemID { get; set; }
    public virtual HolterTest HolterTest { get; set; }
    public string Status { get; set; }
    public virtual List<WorkTask> WorkTasks { get; set; }
    public bool IsStarted { get; set; }
    public bool IsCompleted { get; set; }
    public System.Nullable<DateTime> CompletedDateTime { get; set; }

}

A simple function for accessing the work items by patient number is this:

IEnumerable<WorkItem> GetWorkIncompleteWorkItemsByPatient(string patientNumber)
{
  var db = new YourContext();
  return db.WorkItems.Where(wi => wi.IsCompleted == false && wi.HolterTest.PatientNumber == patientNumber);
}

Then to work on the related tasks, you would access it through the task, in this example if you knew the task ID:

var workTask = YourWorkItem.WorkTasks.FirstOrDefault(wt => wt.WorkTaskID == worktaskId);

You could look through all the tasks in the work item like this:

foreach (var workTask in YourWorkItem.WorkTasks)
{
  //your logic here...
}
于 2013-02-21T16:15:31.623 回答
1

Linq 到实体解释 http://msdn.microsoft.com/en-us/library/bb399367.aspx

但从这里开始可能更适合:EF 主站点http://msdn.microsoft.com/en-us/data/ee712907

如果你真的想直接尝试这个视频和示例代码。http://msdn.microsoft.com/en-us/data/jj193542 然后看到这个http://msdn.microsoft.com/en-us/data/jj573936

基本上基于您拥有的 POCO,您可以读取每个 POCO 并以这种方式获取数据。但是,如果 POCOS 定义了导航属性和外键,则 EF 会做很多繁重的工作。值得重新审视 POCO 定义和 Code-First 模式。

于 2013-02-21T15:36:50.300 回答