可为空的对象必须在连接表查询中具有值
Nullable object must have a value in join tables query
我有三个 table TableTeam
、TableUser
和 TableLink
。我想加入他们得到一个列表。
var teams = _db.TableTeam;
var users = _db.TableUser;
var managerIds = teams.Select(x=>x.ManagerId).ToList();
var list = (from user in _db.TableUser
where user.EndDate == null && user.ManagerId != null && managerIds.Contains(user.ManagerId.Value)
let r = _db.TableLink.FirstOrDefault(l => l.ProjectId == projectId && l.UserId == user.UserId)
let t = _db.TableTeam.FirstOrDefault(m => m.ManagerId == user.ManagerId)
select new MyDto
{
ProjectId = projectId,
UserId = user.UserId,
IsAssigned = r.AssignedId > 0 ?true : false,
AssignedId = r.AssignedId,
Percent = r.Percent.HasValue ? (r.Percent.Value).ToString() + "%": ""
}).GroupBy(d => d.UserId).ToList();
对于tableTableLink
,entity framework中对应的dataSetclass是:
public LinkDto
{
public int AssignedId {get;set;}
public short ProjectId {get;set;}
public short UserId {get;set;}
public decimal? Percent {get;set;}
// ...
}
但是我得到了错误:
Nullable object must have a value
我认为可能是decimal? Percent
造成的。但不确定如何修复它。我也可以使用 store d 过程,这意味着如果你可以通过 SQL 查询解决它,那也很好。
事实证明,我必须在 where
子句之前移动 let
代码,然后将条件添加到 where
。
比如
var list = (from user in _db.TableUser
let r = _db.TableLink.FirstOrDefault(l => l.ProjectId == projectId && l.UserId == user.UserId)
let t = _db.TableTeam.FirstOrDefault(m => m.ManagerId == user.ManagerId)
where user.EndDate == null && user.ManagerId != null && managerIds.Contains(user.ManagerId.Value)
&& r != null && t != null
select new MyDto
{
ProjectId = projectId,
UserId = user.UserId,
IsAssigned = r.AssignedId > 0 ?true : false,
AssignedId = r.AssignedId,
Percent = r.Percent.HasValue ? (r.Percent.Value).ToString() + "%": ""
}).GroupBy(d => d.UserId).ToList();
我有三个 table TableTeam
、TableUser
和 TableLink
。我想加入他们得到一个列表。
var teams = _db.TableTeam;
var users = _db.TableUser;
var managerIds = teams.Select(x=>x.ManagerId).ToList();
var list = (from user in _db.TableUser
where user.EndDate == null && user.ManagerId != null && managerIds.Contains(user.ManagerId.Value)
let r = _db.TableLink.FirstOrDefault(l => l.ProjectId == projectId && l.UserId == user.UserId)
let t = _db.TableTeam.FirstOrDefault(m => m.ManagerId == user.ManagerId)
select new MyDto
{
ProjectId = projectId,
UserId = user.UserId,
IsAssigned = r.AssignedId > 0 ?true : false,
AssignedId = r.AssignedId,
Percent = r.Percent.HasValue ? (r.Percent.Value).ToString() + "%": ""
}).GroupBy(d => d.UserId).ToList();
对于tableTableLink
,entity framework中对应的dataSetclass是:
public LinkDto
{
public int AssignedId {get;set;}
public short ProjectId {get;set;}
public short UserId {get;set;}
public decimal? Percent {get;set;}
// ...
}
但是我得到了错误:
Nullable object must have a value
我认为可能是decimal? Percent
造成的。但不确定如何修复它。我也可以使用 store d 过程,这意味着如果你可以通过 SQL 查询解决它,那也很好。
事实证明,我必须在 where
子句之前移动 let
代码,然后将条件添加到 where
。
比如
var list = (from user in _db.TableUser
let r = _db.TableLink.FirstOrDefault(l => l.ProjectId == projectId && l.UserId == user.UserId)
let t = _db.TableTeam.FirstOrDefault(m => m.ManagerId == user.ManagerId)
where user.EndDate == null && user.ManagerId != null && managerIds.Contains(user.ManagerId.Value)
&& r != null && t != null
select new MyDto
{
ProjectId = projectId,
UserId = user.UserId,
IsAssigned = r.AssignedId > 0 ?true : false,
AssignedId = r.AssignedId,
Percent = r.Percent.HasValue ? (r.Percent.Value).ToString() + "%": ""
}).GroupBy(d => d.UserId).ToList();