当表为空时,Sequence不包含匹配元素错误

时间:2015-09-04 13:22:50

标签: c# asp.net-mvc linq

当表为空时,我收到此错误。也就是说,当模型没有值时,我收到此错误:

  

序列不包含匹配元素

我怎么能在这里放一些逻辑来摆脱它?

问题在于:

var emp = model.vm.Single(i => i.JobseekerId == id);

我试过的是:

public ActionResult JobSeekerProfile(ViewModel model)
{
    if (Session["LogedUserID"] != null)
    {
        JobContext jcntxt = new JobContext();
        int id = Convert.ToInt32(Session["LogedUserID"]);
        model.vm = from jr in jcntxt.JobseekerRegestrations
                   join jc in jcntxt.JobseekerContactdetails on jr.JobseekerId equals jc.JobseekerId
                   join jb in jcntxt.JobseekerBackgroundDetails on jc.JobseekerId equals jb.JobseekerId
                   join jj in jcntxt.JobseekerJobDetails on jc.JobseekerId equals jj.JobseekerId
                   select new ViewModel
                   {
                       JobseekerId = jr.JobseekerId,
                       FirstName = jc.FirstName,
                       LastName = jc.LastName,
                       EmailID = jc.EmailID,
                       JobTitle = jj.JobTitle,
                       Location1 = jj.Location1,
                       University = jb.University,
                       TechnicalExp = jb.TechnicalExp,
                       WorkField = jb.WorkField
                   };

        var job = model.vm.Single(i => i.JobseekerId == id);
        model.FirstName = job.FirstName;
        model.LastName = job.LastName;
        model.EmailID = job.EmailID;
        model.JobTitle = job.JobTitle;
        model.Location1 = job.Location1;
        model.University = job.University;
        model.TechnicalExp = job.TechnicalExp;
        model.WorkField = job.WorkField;
        return View(model);
    }
    else
    {
        return RedirectToAction("JobSeekerLogin");
    }
}

视图

@model Sample.Models.ViewModel
<div class="row">
    <div class=" col-md-12 col-lg-12 ">
        <table class="table table-user-information">
            <tbody>
                <tr>
                    <td>User Name:</td>
                    <td>@Model.FirstName @Model.LastName</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

2 个答案:

答案 0 :(得分:4)

使用SingleOrDefault方法,然后检查结果对象是否为null

var job = model.vm.Where(i => i.JobseekerId == id).SingleOrDefault();
if (job != null)
{
    model.FirstName = job.FirstName;
    model.LastName = job.LastName;
    model.EmailID = job.EmailID;
    model.JobTitle = job.JobTitle;
    model.Location1 = job.Location1;                
    model.University = job.University;
    model.TechnicalExp = job.TechnicalExp;
    model.WorkField = job.WorkField;
}

答案 1 :(得分:3)

Single期望总是我们只有1个结果。零或大于1将导致异常。

也许您想要使用SingleOrDefault,但是一旦这样做job可能为空 - 在那种情况下,您的下一行会出错NullReferenceException - 所以请确保陷阱的是:

var job = model.vm.SingleOrDefault(i => i.JobseekerId == id);
if (job != null)
{
    model.FirstName = job.FirstName;
    model.LastName = job.LastName;
    model.EmailID = job.EmailID;
    model.JobTitle = job.JobTitle;
    model.Location1 = job.Location1;                
    model.University = job.University;
    model.TechnicalExp = job.TechnicalExp;
    model.WorkField = job.WorkField;
}