0

iam new in linq, i want to convert linq result to dictionary, but i get an error.

this is my c# code :

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

this is my error :

Cannot implicitly convert type 'System.Collections.Generic.Dictionary<string,AnonymousType#1>' to 'System.Collections.Generic.Dictionary<string,DataAccess.TT_ABSENCE>'

how to convert linq result to dictionary?

7
  • 2
    Replace this select new { abs.ReceivedTime, abs.Employee_NIP, abs.AbsenceTime, abs.GeoLoc, abs.statusAbsence, emp.Employee_Name } with this select abs Commented Sep 11, 2014 at 3:51
  • still error Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement Commented Sep 11, 2014 at 3:54
  • oh okay, its works :D Commented Sep 11, 2014 at 3:56
  • but i cant get employeeName with one query, how to get it? Commented Sep 11, 2014 at 3:58
  • That wasn't what you asked for. Commented Sep 11, 2014 at 3:59

1 Answer 1

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); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.