linq to dictionary不能隐式转换类型

时间:2014-09-11 03:48:38

标签: c# linq dictionary

iam new in linq,我想将linq结果转换为字典,但是我收到错误。

这是我的c#代码:

var q = from abs in db.TT_ABSENCEs
        join emp in db.TT_EMPLOYEEs on abs.Employee_NIP equals emp.Employee_NIP
        where
        Convert.ToDateTime(abs.ReceivedTime).Day == DateTime.Now.Day
        && Convert.ToDateTime(abs.ReceivedTime).Month == DateTime.Now.Month
                && Convert.ToDateTime(abs.ReceivedTime).Year == DateTime.Now.Year
        select new { abs.ReceivedTime, abs.Employee_NIP, abs.AbsenceTime, abs.GeoLoc, abs.statusAbsence, emp.Employee_Name };


Dictionary<String, TT_ABSENCE> _dict =
    q.ToDictionary(abs => abs.Employee_NIP, abs => abs.statusAbsence);

这是我的错误:

  

无法隐式转换类型   'System.Collections.Generic.Dictionary<string,AnonymousType#1>'到   'System.Collections.Generic.Dictionary<string,DataAccess.TT_ABSENCE>'

如何将linq结果转换为字典?

1 个答案:

答案 0 :(得分:2)

var q = from abs in db.TT_ABSENCEs
        join emp in db.TT_EMPLOYEEs on abs.Employee_NIP equals emp.Employee_NIP
        where
        Convert.ToDateTime(abs.ReceivedTime).Day == DateTime.Now.Day
        && Convert.ToDateTime(abs.ReceivedTime).Month == DateTime.Now.Month
                && Convert.ToDateTime(abs.ReceivedTime).Year == DateTime.Now.Year
        select abs;


Dictionary<String, TT_ABSENCE> _dict =  q.ToDictionary(abs => abs.Employee_NIP);