0

Consider these two tables:

 ClassID  Name
   1       C1
   2       C2

 ClassID  List<CourseSession>
   1            [Object that has value "A"], [Object that has value "B"]
   2            [Object that has value "B"], [Object that has value "C"]

When I join these two tables in Linq, I get:

ID Name List
1   C1  [A, B]
2   C2  [A, B]

Wheras I need to expand them:

ID Name List
1   C1  A
1   C1  B
2   C2  A
2   C2  B

Linq code:

    var classes = from row in t.AsEnumerable() 
                               select new
                                  {
                                      ClassID = row.Field<Guid>("ClassID"),
                                      ClassName = row.Field<string>("Name"),
                                  };

    var classCourses = from row in classes.AsEnumerable()
                                   select new 
                                   {
                                       ID = row.ID,
                                       CourseSessionList = GetAllCoursesByID(row.ID).AsEnumerable()
                                   };

    //Attempt to join
    var expandedClassCourse = from classRow in classes 
                                   join ccRow in classCourses 
                                   on classRow.ID equals ccRow.ID
                                    into filteredExpandedClasses
                                   select filteredExpandedClasses;

I'm not sure how to achieve this. Any ideas?

3 Answers 3

1

Something like (not sure what your model looks like):

context.CouseSessions.Where(cs => /* condition goes here */)
    .Select(cs =>
        new
        {
            Name = cs.Name,
            Class = cs.Class.Name
        });

or

context.Classes.Where(c => /* condition goes here */)
    .SelectMany(c => c.Courses)
    .Select(cs =>
        new
        {
            Name = cs.Name,
            Class = cs.Class.Name
        });
Sign up to request clarification or add additional context in comments.

Comments

1

I created two models based on assumption. I hope this helps.

class Info
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<string> List { get; set; }
    }

    class MyClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string s { get; set; }
    }

    static void Main(string[] args)
    {
        var infos = new List<Info> { new Info { Id = 1, Name = "c1", List = new List<string> { "A", "B" } }, new Info { Id = 2, Name = "c2", List = new List<string> { "A", "B" } } };
        var myClasses = new List<MyClass>();
        foreach (var info in infos)
        {
            myClasses.AddRange(info.List.Select(a => new MyClass { Id = info.Id, Name = info.Name, s = a }));
        }
    }

Comments

0
(from c in classList
join s in sessionList on c.ClassID equals s.ClassID
select new
{
    ID = c.ClassID,
    Name = c.Name,
    SessionList = s.SessionList
})
.SelectMany(e => e.SessionList.Select(s => new
{
    ID = e.ClassID,
    Name = e.Name,
    Session = s
}))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.