你好我有这样的linq查询:
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
}
),
};
问题是,我只想为HK2
选择一条记录并在子查询中分配给Mandays但是下面有一个错误:
无法隐式转换类型 &#39; System.Linq.IQueryable&#39;到十进制&#39;
解决方案是什么?谢谢
答案 0 :(得分:4)
而不是返回匿名类(function show_user($data){
$this->db->where('pkid', $data);
$query = $this->db->get('users');
$query_result = $query->result();
return $query_result;
}
function show($data){
$this->db->select('*');
$this->db->from('users');
$this->db->where('pkid', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}
// Update Query For Selected Student
function update_user($id,$data){
$this->db->where('pkid', $id);
$this->db->update('users', $data);
}
)
new {...}
返回您想要的 select new
{
Mandays = g.FirstOrDefault().ManDays <-- this is decimal type
}
:
Decimal
答案 1 :(得分:1)
您应该在最后添加另一个FirstOrDefault()
并使用简单值而不是匿名类型:
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
};