Cross Join with Where clause

偶尔善良 提交于 2019-12-13 19:14:20

问题


In Linq i must create a query with method only, i've got 2 tables :

  • Students (LastName, FirstName, Result)
  • Grades (Max, Min, Name)

I must select students (LastName, FirstName) and add to it the grade (Result > Min && Result < Max).

In the end I must have :

IEnumerable<T> T => LastName, FirstName, Grade

I try this :

var SAG = dc.Students
            .Where(w => w.Year_Result >= 12)
            .Join(dc.Grades, s => true, g => true, (s, g) => 
                  new { s.LastName, 
                        s.FirstName, 
                        Grade = g.Name
                                 .Where(w => (w.Min < s.Result) 
                                          && (w.Max > s.Result))
                        .FirstOrDefault() }).ToList();

But with this request I've only 2 results but I must have 40 results.


回答1:


Would this work for you?

var SAG =
    from s in dc.Students
    from g in dc.Grades
    where g.Min < s.Result
    where g.Max > s.Result
    select new
    {
        s.LastName, s.FirstName, Grade = g.Name,
    };

(I do suspect you need either <= or >= in there somewhere.)



来源:https://stackoverflow.com/questions/21659325/cross-join-with-where-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!