将模型(linq)结果传递给预先声明的变量

时间:2013-11-18 20:42:19

标签: asp.net-mvc asp.net-mvc-3 linq

我想声明一个模型变量(我相信它是模型的可枚举列表,然后将结果设置为该变量。

而不是这样做......

if (bIncludeGlobal)
                {
                    //include records on GLOBAL no-call
                     var model = (
                     from p in dbMaster.tblNoCallLists
                     where ((p.ProjectCode == SessionVars.Session_ProjectCode || p.ProjectCode == "GLOBAL") && p.MRN.Contains(psMRN))
                     orderby (p.AutoNumber) descending
                     select p).ToList().Take(100);
                    if (model.Count() > 0)
                    {
                        return View(model);
                    }
                    else
                    {
                        return RedirectToAction("Create", new { phonenumber = psPhoneNumber });
                    }
                }
                else
                {
                    var model = (
                     from p in dbMaster.tblNoCallLists
                     where (p.ProjectCode == SessionVars.Session_ProjectCode &&  p.MRN.Contains(psMRN))
                     orderby (p.AutoNumber) descending
                     select p).ToList().Take(100);
                    if (model.Count() > 0)
                    {
                        return View(model);
                    }
                    else
                    {
                        return RedirectToAction("Create", new { phonenumber = psPhoneNumber });
                    }
                }

我想做这样的事情......(基本上在linq语句之前声明模型变量)

    if (bIncludeGlobal)
var model; //NOT SURE HOW TO DECLARE THIS
                    {
                        //include records on GLOBAL no-call
                         model = (
                         from p in dbMaster.tblNoCallLists
                         where ((p.ProjectCode == SessionVars.Session_ProjectCode || p.ProjectCode == "GLOBAL") && p.MRN.Contains(psMRN))
                         orderby (p.AutoNumber) descending
                         select p).ToList().Take(100);
                                            }
                    else
                    {
                        var model = (
                         from p in dbMaster.tblNoCallLists
                         where (p.ProjectCode == SessionVars.Session_ProjectCode &&  p.MRN.Contains(psMRN))
                         orderby (p.AutoNumber) descending
                         select p).ToList().Take(100);
                                            }
if (model.Count() > 0)
                        {
                            return View(model);
                        }
                        else
                        {
                            return RedirectToAction("Create", new { phonenumber = psPhoneNumber });
                        }

1 个答案:

答案 0 :(得分:2)

var model = (bIncludeGlobal)
             ? (from p in dbMaster.tblNoCallLists
                 where ((p.ProjectCode == SessionVars.Session_ProjectCode || p.ProjectCode == "GLOBAL") && p.MRN.Contains(psMRN))
                 orderby (p.AutoNumber) descending
                 select p).ToList().Take(100)
             : (from p in dbMaster.tblNoCallLists
                 where (p.ProjectCode == SessionVars.Session_ProjectCode &&  p.MRN.Contains(psMRN))
                 orderby (p.AutoNumber) descending
                 select p).ToList().Take(100);

return (model.Count() > 0)
           ? View(model)
           : RedirectToAction("Create", new { phonenumber = psPhoneNumber });