具有两个表的筛选器列表在实体框架中连接数据

时间:2014-04-25 12:04:15

标签: linq entity-framework

我有两张桌子..

Student (StudentId,Name,FatherName)
Qualification (QualificationId,StudentId,DegreeName)

我有这样的数据..

var myList = (from c in entities.Students
              join q in entities.Qualifications on c.StudentId equals  q.StudentId
              select new {c.Name,c.FatherName,q.DegreeName}).ToList();

现在我想更多地过滤myList ..我怎么能这样做,比如..

var filteredList = myList.Select(c=> new Student 
{
Name=c.Name,
FatherName=c.FatherName
//Degree=C.Degree
}).ToList();

如果我想获得DegreeName,上面的Linq查询无效,我的问题是如何进一步过滤myList。 感谢。

1 个答案:

答案 0 :(得分:2)

var filteredList = myList.Where(i => i.FatherName == "Shahid").ToList();

请记住,因为您在原始查询中调用了ToList(),您现在正在内存中进行过滤。如果要在数据库中进行过滤,请在第一个查询中删除ToList()并按以下方式执行:

var myList = from c in entities.Students 
             join q in entities.Qualifications on c.StudentId     equals  q.StudentId
             select new {
                 c.Name,
                 c.FatherName,
                 q.DegreeName
             };

var filteredInDatabase = myList.Where(i => i.FatherName == "Shahid").ToList();