English 中文(简体)
更高效 LINQ JOIN
原标题:More Efficient LINQ JOIN
  • 时间:2012-04-20 13:17:06
  •  标签:
  • linq
  • join

我试图加入2个表格,其中一张可以链接,但我只想把“一”一栏数据放在一行中,在所有其他各行中,这种数据应一律无效。 因此:

TEACHER
---------
TeacherID
TeacherBiography
.
.
.

STUDENT
-----------
TeacherID
StudentFName
StudentLName
.
.
.

例: 你们想让所有第一名学生Joe,通过教师培训加入教师队伍,但限制退学结果,以便教师数据不会在每行复课。 原因是教师比照率很高;我需要回来,但不是每行。

因此,一些样本产出应当像:

------------------------------------------------
StudentFName | StudentLName | TeacherID | TeacherBiography
------------------------------------------------
 Joe         |  Smith       | 1         |  long biography for teacher 1..   
 Joe         |  Jones       | 2         |  long biography for teacher 2..   
 Joe         |  Michaels    | 1         | null
 Joe         |  Rogers      | 3         |  long biography for teacher 3..  
 Joe         |  Washington  | 1         | null
.
.
.
.

So in the case of Michaels and Washington the TeacherBiograph (and all other teacher columns) is null because the data was already returned in the Smith row.

我如何这样做?

- 法 国

最佳回答

I think the only efficient way to do this is to Join STUDENTs and TEACHERs with the TeacherID as a key, then you can output this key only one time for only one row and the other rows for the same key output null instead, like this:

var StudentsList = students.Join(  //Inner join
    Teachers,
    s => s.TeacherID,
    t => t.TeacherID,
    (teacher, teacherStudetns) => 
        new
        {
            Teacher = teacher,
            HisStudents = teacherStudetns
        });

然后,你只能一度完成关键产出,例如:

foreach(var item in StudentsList)
{
     Console.Writline("Teach Name: {0}, His students:", item.TeacherBiography);
     foreach(var student in item.HisStudents)
     {
         Console.writeLine("---------- {0}", student.StudentName);
     }
}
问题回答

我将在两个步骤中这样做,而不是使你的守则复杂化,试图在其中做到这一点。 首先,检索你所关心的学生信息。 如果有的话,鉴于教师信息数据库的独特清单,你可以再做一次问询,了解教师的信息。

var students = //student query
var teacherIds = students.Select(p => p.TeacherID).Distinct();
var teachers = teacherTable.Where(p => p.Contains(teacherIds));

然后,你可以怀念这两个成果。

var allData = from s in students
    join t in teachers on
    t.teacherID equals s.TeacherID
    select new { StudentName = s.Name, TeacherBio = t.TeacherBiography};

这将造成两个数据库的点击,但你只将所需要的数据项目退回。





相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签