3.5 VS 4.0.NET Framework

5 framework, without using a foreign key relationship in the database, and I wonder how 4.0 can improve this garbage code that I need to pass back through several objects after several table joins.

  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;
            }
        }
    }

3 objects are returned in the GUI. However, for this I need to add the property Public CourseByTutors {get; set} and the public course (get; set;) in the User class, which I found to ruin my code. In this case, how could 4.0 solve this? I read something about choosing tupel .. ??

+3
source share
3 answers

, , 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;

, .

+2

( 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: CourseTutor.Name Course.Id. , , User CourseTutor Course.

+3

, , - , , . 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;
        }
    }
}
0
source

Source: https://habr.com/ru/post/1795062/


All Articles