本文介绍了LINQ查询的EF 4.1数据嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用WPF和EF 4.2致力于建立一个桌面应用程序。我有3个实体已级联一到多的关系,学生有零或更多的学校。学校有零或更多的课程参加。我需要显示的数据分组列表显示了学生,他们所有的学校,然后所有的课程。

 大学生课外班
INT编号INT标识INT标识
字符串名称字符串名称弦乐主题
INT yearBorn字符串地址INT积分
IList的<学校> IList的<类>
 

我被如何塑造数据难住了。

第一个问题是,EF 4.2隐藏在导航实体的ID对象,所以我不能够按组将它们用作键的。

接下来的问题是我不知道如何将各组第二级嵌套,因为我还需要返回对象的属性以及像Student.Name或School.Address的方式。

解决方案

您应该不需要分组方式 - 只是检索实体在当前对象图

本标准是一样的东西。

  VAR上下文=新的EntityContext();
。VAR学生= context.Students.Include(学校),包括(Schools.Classes);
返回的学生;
 

这将返回一个包含了一组学生,每个学生都包含一组学校,每个学校都包含一组类的对象图。在分组是由EF完成 - 这是一个ORM,它映射关系数据库表到对象在code的目的

I'm working with WPF and EF 4.2 to build a desktop application. I have 3 entities which have cascading one-to-many relationships, Students have zero-or-more schools. Schools have zero-or-more classes attended. I need to display the data in a grouped list showing the student, all their schools then all their classes.

Students          School            Class
int Id            int Id            int Id
string Name       string Name       string Subject
int yearBorn      string Address    int Credits
IList<School>     IList<Class>

I'm stumped by how to shape the data.

The first issue is that EF 4.2 hides the Id of the entities in Navigation objects so I cannot use them as keys in a "group by".

The next issue is I'm not sure how to include the second level nesting in the groups since I also need to return attributes of objects along the way like Student.Name or School.Address.

解决方案

You shouldn't need to Group By - just retrieve the entities in their current object graph.

The standard is something like

var context = new EntityContext();
var students = context.Students.Include("Schools").Include("Schools.Classes");
return students;

This returns an objects graph containing a set of students, where each students contains a set of schools, where each school contains a set of classes. The 'grouping' is done by EF - this is the purpose of an ORM, which maps relational database tables into objects in your code.

这篇关于LINQ查询的EF 4.1数据嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 18:57