3.5 VS 4.0 .NET框架
5 框架,不在数据库中使用关系外键,我想知道 4.0 如何改进这些垃圾代码,我需要在多次连接表后传回多个对象。
public IList<User> GetTutorByCourseId(int courseId)
{
IList<User> output = new List<User>();
using (leDataContext db = new leDataContext())
{
try
{
var m = from c in db.Courses
join ct in db.CourseByTutors on c.Id equals ct.CourseId
join u in db.Users on ct.TutorId equals u.Id
where c.Id == courseId
select new
{
c, ct, u
};
foreach (var result in m)
{
User user = new User();
user.Id = result.u.Id;
user.Name = result.u.Name;
user.CourseTutor.Id = result.ct.Id;
user.Course.Name = result.c.Name;
output.Add(user);
}
return output;
}
catch (Exception ex)
{
Logger.Error(typeof(User), ex.ToString());
throw;
}
}
}
GUI 中有 3 个对象返回给调用者。但是,要做到这一点,我必须在 User 类中添加 public CourseByTutors{get;set} 和 public Course(get;set;) 的属性,我发现这会弄乱我的代码。那么4.0如何解决这个问题呢?我读过一些关于选择 tupel 的内容..?
5 framework, not using relationship foreign key in database, and i wonder how 4.0 can improve this junk of code which i need to pass back the multiple object after multiple joins of tables.
public IList<User> GetTutorByCourseId(int courseId)
{
IList<User> output = new List<User>();
using (leDataContext db = new leDataContext())
{
try
{
var m = from c in db.Courses
join ct in db.CourseByTutors on c.Id equals ct.CourseId
join u in db.Users on ct.TutorId equals u.Id
where c.Id == courseId
select new
{
c, ct, u
};
foreach (var result in m)
{
User user = new User();
user.Id = result.u.Id;
user.Name = result.u.Name;
user.CourseTutor.Id = result.ct.Id;
user.Course.Name = result.c.Name;
output.Add(user);
}
return output;
}
catch (Exception ex)
{
Logger.Error(typeof(User), ex.ToString());
throw;
}
}
}
There are 3 objects being return to the caller in GUI. However, to do this i got to add the property of public CourseByTutors{get;set} and the public Course(get;set;) in the User class which i find that it will mess up my code. In this case, how would 4.0 able to solve this? i read something about select tupel .. ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个(3.5 中)怎么样?
编辑:替换了非法的
CourseTutor.Name
和Course.Id
初始值设定项。假设User
的构造函数没有对CourseTutor
和Course
进行任何花哨的初始化,更正后的代码将起作用。What about this (in 3.5)?
EDIT: Replaced illegal
CourseTutor.Name
andCourse.Id
initializers. The corrected code will work, assuming that the constructor ofUser
didn't do any fancy initialization ofCourseTutor
andCourse
.即使数据库中没有外键,也没有理由不能将关系添加到 EF 模型中。这将有助于简化问题,因为您不必生成其他属性来存储子值; CourseTutor、CourseName 等。3.5
和 4.0 都可以在这里提供帮助,但根据我的经验,这在 4.0 中要容易得多。
一旦您的上面的代码可能看起来像这样:
希望这会有所帮助。
Even though you don't have foreign keys in the database, there's no reason that you can't add relationships into your EF model. This will help simplify the problem as you will not have to generate additional properties to store your child values; CourseTutor, CourseName etc..
Both 3.5 and 4.0 can help out here, although, in my experience this is much easier in 4.0.
Once the your code above might look something like:
Hope this helps.
我通常处理这个问题的方法是从 GUI 的数据实体中获得一个单独的模型,其中仅包含 GUI 所需的信息。如果你愿意的话,你可以在 3.5 中做到这一点。
The way that I would normally handle this is to have a separate model from my data entities for the GUI that includes just the information that the GUI needs. You could do this in 3.5 if you want.