Linq查询将三个不同实体的值从矩阵中获取

时间:2015-08-26 19:26:07

标签: c# .net linq entity-framework lambda

我正在尝试编写Linq查询以使用EF从三个表中获取数据并且很难使用它....我所拥有的是

tblUsername
userId      username
---------------------
 1          jadam
 2          janeb
 3          peterk
 4          rickd

第二个表有列的名称

tablcolumns
=============
colId     colname
-----------------
  1        Name
  2        Monday
  3        Tuesday
  4        Wednesday
  5        Thursday
  6        Friday
  7        Saturday
  8        Sunday

第三个表有值

tblSchedule
==================
id   userId   colId   hrs
--------------------------
1      2       3       4
2      3       7       7.5
3      1       8       2 

我正在尝试编写Linq语句,以便我的输出看起来像这样

public class model 
{
    public int colId {get; set;}
    public string colname {get; set;}
    public virtual IQueryable<detailsmodel> detailsmodel {get; set;}

{ 

public class detailsmodel
{
     public int userId {get; set;}
     public string username {get; set:}
     pubic double hrs {get; set;}
}

结果应具有以下结构

Name  Monday  Tuesday  Wednesday Thursday Friday Saturday Sunday 
jadam   2
janeb           4
peterk                                            7.5
rickd

我试过了,但它没有用

var result= (DbContext.Set<tablcolumns>() 
            .Select(c=> new  model()
               {
                  colId=c.colId,
                  colname=c.colname,
                  detailsmodel= (from e in DbContext.Set<tblSchedule>(){
                                  join a in DbContext.Set<tblUsername>() 
                                      on e.userId equals a.userId
                                  Select new detailsmodel()
                                   {userId=a.username,
                                    username=a.username,
                                    hrs= e.hrs}
                                  }) });

上面的查询给出了一列以下的值,其余的列都是空的。 请让我知道如何获得Linq查询权限以获得结果,如上面结果结构所示。

0 个答案:

没有答案
相关问题