1

我一直在尝试很多方法,但它不断给我下面的代码这个错误

using (custTrans db = new custTrans())
{
    Guid AgentGUID = Guid.Parse(AgentUID);
    var AgentQuery = from a in db.Agents
                     join u in db.UserLogins on a.UserUID equals u.UserUID into j
                     from u in j.DefaultIfEmpty()
                     where a.AgentUID == AgentGUID
                     select new { a.AgentUID  , a.Name, u.Login, u.UserUID, u.Type };

    foreach (var result in AgentQuery)
    {
        txtName.Text = result.Name;
        txtUsername.Text = result.Name
    }
}

当我尝试加载记录时,我不断收到此错误

转换为值类型“Guid”失败,因为具体化值为 null。结果类型的泛型参数或查询必须使用可为空的类型。

任何人都知道如何解决这个问题?

4

1 回答 1

1

经过一些建议后,我对代码进行了一些更改

 Guid AgentGUID = Guid.Parse(AgentUID);

 var AgentQuery = from a in db.Agents
                  join u in db.UserLogins on a.AgentUID equals u.UserUID into j
                  from u in j.DefaultIfEmpty()
                  where a.AgentUID == AgentGUID
                  select new { a.AgentUID, a.Name, Login = (u == null ? String.Empty : u.Login), UserUID = ( u == null ? Guid.Empty : u.UserUID), Type = (u == null ? -1 : u.Type) };

foreach (var result in AgentQuery)
{
    txtName.Text = result.Name;
    txtUsername.Text = result.Name;

    if (!string.IsNullOrEmpty(result.Login.ToString()))
    {
        chkEnableLogin.Checked = true;
        txtUsername.Text = result.Login;
        txtUsername.ReadOnly = true;
        ddlType.SelectedValue = result.Type.ToString();
        ViewState.Add("UserUID", result.UserUID);
    }
}

用于检查 Null 的 select 语句实际上起到了作用。感谢所有给我提示的人

于 2013-10-16T14:19:15.353 回答