LINQ查询使用if条件和&&手术

时间:2014-04-29 09:39:44

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

搜索控制器方法

 [HttpPost]
    public ActionResult Search(string cno, string fname, string lname, string male, string female, string istateid, string icityid, string professionid, string educationid)
    {

        var db = new clubDataContext();
        var query = db.M_Registarions.Where((x=> x.M_ContactNo ==(cno??x.M_ContactNo) && x.M_Fname == (fname ?? x.M_Fname) && x.M_Lname==(lname ?? x.M_Lname) && x.M_Gender == (male??x.M_Gender));
        if(istateid != "")
        {
            int stateid1 = Convert.ToInt32(istateid);
             query = query.Where(x=> x.M_StatteId == stateid1);
        }
        if(icityid != "")
        {
            int cityid1 = Convert.ToInt32(icityid);
             query = query.Where(x=> x.M_CityId == cityid1);
        }
         if(professionid != "")
         {
            int professionid1 = Convert.ToInt32(professionid);
            query = query.Where(x=> x.P_id == professionid1);
         }
         if(educationid != "")
         {
         int educationid1 = Convert.ToInt32(educationid);
         query = query.Where(x=> x.P_id == educationid1);
         }
         if (!query.Any())
         {
             ViewBag.Count = 0;
         }
         else
         {
             var result = query.ToList();

             //var search_query = from p in db.M_Registarions where (x => x.M_Fname.Contains(fname ?? x.M_Fname) || x.M_Lname.Contains(lname ?? x.M_Lname)) select p;
             ViewBag.SearchResult = result;
             ViewBag.Count = 1;
         }
            return PartialView("SearchResult");
    }

这里我在cno,名字和姓氏中使用OR操作。我想使用&&(And)操作而不是||(OR)。但问题是我必须检查NULL或空格,我已经在stateid,cityid,professionid和educationid中完成了。 那么首先如何使用条件? var query = db.M_Registration.Where(if {})喜欢这个吗?

3 个答案:

答案 0 :(得分:0)

使用blocs {}和"返回"提出一个更复杂的表达

例如:

db.M_Registration.Where(p => 
                    {
                        if{ // my condition... }
                            return p.Name == name
                        else
                            return p.Name == ...
                    });

答案 1 :(得分:0)

你可以试试这个。

[HttpPost]
public ActionResult Search(string cno, string fname, string lname, string male, string female, string istateid, string icityid, string professionid, string educationid)
{

    var db = new clubDataContext();
    var query = db.M_Registarions.Where((x=> x.M_ContactNo ==(cno??x.M_ContactNo) && x.M_Fname == (fname ?? x.M_Fname) && x.M_Lname==(lname ?? x.M_Lname) && x.M_Gender == (male??x.M_Gender));
    if(IntValue(istateid).HasValue)
    {
         query = query.Where(x=> x.M_StatteId == IntValue(istateid).Value);
    }
    if(IntValue(icityid).HasValue)
    {
         query = query.Where(x=> x.M_CityId == IntValue(icityid).Value);
    }
     if(IntValue(professionid).HasValue)
     {
        query = query.Where(x=> x.P_id == IntValue(professionid).Value);
     }
     if(IntValue(educationid).HasValue)
     {
     query = query.Where(x=> x.P_id == IntValue(educationid).Value);
     }
     if (!query.Any())
     {
         ViewBag.Count = 0;
     }
     else
     {
         var result = query.ToList();

         //var search_query = from p in db.M_Registarions where (x => x.M_Fname.Contains(fname ?? x.M_Fname) || x.M_Lname.Contains(lname ?? x.M_Lname)) select p;
         ViewBag.SearchResult = result;
         ViewBag.Count = 1;
     }
        return PartialView("SearchResult");
}

    public int? IntValue(string input) 
    {
        int temp;

        if (int.TryParse(input, out temp)) 
        {
            return temp;
        }

        return null;
    }

答案 2 :(得分:0)

我认为您可以通过尝试以下查询来获得您想要的结果并避免大量的ifs:

var query =
        db.M_Registarions.Where(
            x =>
                x.M_ContactNo == (cno ?? x.M_ContactNo) && x.M_Fname == (fname ?? x.M_Fname)
                && x.M_Lname == (lname ?? x.M_Lname) && x.M_Gender == (male ?? x.M_Gender)
                && (string.IsNullOrEmpty(istateid) || x.M_StatteId == Convert.ToInt32(istateid))
                && (string.IsNullOrEmpty(icityid) || x.M_CityId == Convert.ToInt32(icityid))
                && (string.IsNullOrEmpty(professionid) || x.P_id == Convert.ToInt32(professionid))
                && (string.IsNullOrEmpty(educationid) || x.P_id == Convert.ToInt32(educationid)));
相关问题