3.5 VS 4.0 .NET框架

发布于 2024-10-19 17:11:59 字数 1407 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹沉浮 2024-10-26 17:11:59

这个(3.5 中)怎么样?

select new User
{
    Id = u.Id,
    Name = u.Name,
    CourseTutor = new CourseTutor {Id = ct.Id},
    Course = new Course {Name = c.Name}
};
return m.ToList();

编辑:替换了非法的 CourseTutor.NameCourse.Id 初始值设定项。假设 User 的构造函数没有对 CourseTutorCourse 进行任何花哨的初始化,更正后的代码将起作用。

What about this (in 3.5)?

select new User
{
    Id = u.Id,
    Name = u.Name,
    CourseTutor = new CourseTutor {Id = ct.Id},
    Course = new Course {Name = c.Name}
};
return m.ToList();

EDIT: Replaced illegal CourseTutor.Name and Course.Id initializers. The corrected code will work, assuming that the constructor of User didn't do any fancy initialization of CourseTutor and Course.

南城追梦 2024-10-26 17:11:59

即使数据库中没有外键,也没有理由不能将关系添加到 EF 模型中。这将有助于简化问题,因为您不必生成其他属性来存储子值; CourseTutor、CourseName 等。3.5

和 4.0 都可以在这里提供帮助,但根据我的经验,这在 4.0 中要容易得多。

一旦您的上面的代码可能看起来像这样:

var results = (from u in db.Users
        where u.Course.Id == courseId
        select u).ToList();
return results;

希望这会有所帮助。

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:

var results = (from u in db.Users
        where u.Course.Id == courseId
        select u).ToList();
return results;

Hope this helps.

苍景流年 2024-10-26 17:11:59

我通常处理这个问题的方法是从 GUI 的数据实体中获得一个单独的模型,其中仅包含 GUI 所需的信息。如果你愿意的话,你可以在 3.5 中做到这一点。

public class TutorViewModel
{
     public IEnumerable<User> Tutors { get; set; }
     // the pair CourseId, UserId is the relation in CourseTutors so we only
     // need to keep it once, not once per user.
     public int CourseId { get; set; } 
     public string CourseName { get; set; }
}



public TutorViewModel GetTutorByCourseId(int courseId)
{
    var model = new TutorViewModel { CourseId = courseId };

    using (leDataContext db = new leDataContext())
    {
        try
        {
            model.CourseName = db.Courses
                                 .First( c => c.CourseId == courseId )
                                 .Name;
            model.Users = db.CourseByTutors
                            .Where( c => c.Id == courseId )
                            .Join( db.Users,
                                   c => c.TutorId,
                                   u => u.Id,
                                   (c,u) => u );

            return model;
        }
        catch (Exception ex)
        {
            Logger.Error(typeof(User), ex.ToString());
            throw;
        }
    }
}

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.

public class TutorViewModel
{
     public IEnumerable<User> Tutors { get; set; }
     // the pair CourseId, UserId is the relation in CourseTutors so we only
     // need to keep it once, not once per user.
     public int CourseId { get; set; } 
     public string CourseName { get; set; }
}



public TutorViewModel GetTutorByCourseId(int courseId)
{
    var model = new TutorViewModel { CourseId = courseId };

    using (leDataContext db = new leDataContext())
    {
        try
        {
            model.CourseName = db.Courses
                                 .First( c => c.CourseId == courseId )
                                 .Name;
            model.Users = db.CourseByTutors
                            .Where( c => c.Id == courseId )
                            .Join( db.Users,
                                   c => c.TutorId,
                                   u => u.Id,
                                   (c,u) => u );

            return model;
        }
        catch (Exception ex)
        {
            Logger.Error(typeof(User), ex.ToString());
            throw;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文