linq到dictionary无法进行类型的隐式转换

本文关键字:类型 转换 dictionary linq | 更新日期: 2023-09-27 18:21:31

在linq中是新的,我想将linq结果转换为dictionary,但我得到了一个错误。

这是我的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结果转换为字典?

linq到dictionary无法进行类型的隐式转换

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);