Mamen
Mamen

Reputation: 1446

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#6>' to 'decimal'

hallo i have a linq query like this :

var data = from d in db.DailyBonus
                   where d.Date>= startDate1 && d.Date<=endDate1
                   group d by new { Em = d.Employee.Id } into g
                   select new BasisViewModel
                   {
                       EmployeeId = g.Key.Em,
                       EmployeeNumber = g.FirstOrDefault().Employee.EmployeeNumber,
                       FullName = g.FirstOrDefault().Employee.FullName,
                       HK1 = (decimal)g.FirstOrDefault().ManDays,
                       Basis1 = (decimal)g.FirstOrDefault().BonusBase,
                       HK2 = (
                              (from d in db.DailyBonus 
                              where d.Date>= startDate2 && d.Date<=endDate2
                              group d by new { Em = d.Employee.Id } into g2
                              select new 
                              {
                                 Mandays =  g.FirstOrDefault().ManDays <-- this is decimal type
                              }
                             ),
                   };

the problem is , i just want to select one record for HK2 and assigned to Mandays in subquery but there is an error bellow :

Cannot implicitly convert type 'System.Linq.IQueryable' to 'decimal'

what is the solution ? thank you

Upvotes: 0

Views: 1376

Answers (2)

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

You should add another FirstOrDefault() in the end and use simple value instead of anonymous type:

var data = from d in db.DailyBonus
                       where d.Date>= startDate1 && d.Date<=endDate1
                       group d by new { Em = d.Employee.Id } into g
                       select new BasisViewModel
                       {
                           EmployeeId = g.Key.Em,
                           EmployeeNumber = g.FirstOrDefault().Employee.EmployeeNumber,
                           FullName = g.FirstOrDefault().Employee.FullName,
                           HK1 = (decimal)g.FirstOrDefault().ManDays,
                           Basis1 = (decimal)g.FirstOrDefault().BonusBase,
                           HK2 = (
                                  (from d in db.DailyBonus 
                                  where d.Date>= startDate2 && d.Date<=endDate2
                                  group d by new { Em = d.Employee.Id } into g2
                                  select g.FirstOrDefault().ManDays <-- this is simple value now

                                 ).FirstOrDefault(),<--this is another first or default 
                       };

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

Instead of returning an anonymous class (new {...})

  select new 
  {
       Mandays =  g.FirstOrDefault().ManDays <-- this is decimal type
  }

return a Decimal you want:

  select g.FirstOrDefault().ManDays;

Upvotes: 4

Related Questions