如何在ASP.NET MVC3中验证LINQ查询 [英] How to validate a linq query in asp.net mvc3

查看:67
本文介绍了如何在ASP.NET MVC3中验证LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在使用asp.net mvc3.我的任务是在linq查询中使用if子句进行验证.
有人可以帮我吗

这是我的代码

Hi all,
i am using asp.net mvc3.My task is to validate using if clause in linq query.
Could any one please help me out in this

This is my code

public JsonResult Details(string FirstName,string LastName,string City,string State,string Country,
   string Company,string College)
       {
           if (FirstName != null || LastName != null || City != null || State != null || Country != null || Company != null ||  College != null)

               var st = (from i in dbContext.User_details
                         join j in dbContext.User_addresses on i.Userid equals j.Userid
                         join k in dbContext.Userworkdetails on j.Userid equals k.Userid
                         join l in dbContext.Useredudetails on k.Userid equals l.Userid

    where i.Firstname==FirstName && i.Lastname==LastName && j.City==City && j.State==State && j.Country==Country
    && k.Company==Company && l.College==College
                         select new
                         {

                             Id = i.Userid,
                             city = j.City,
                             Firstname = i.Firstname,
                             company = k.Company,
                             college = l.College,
                             state = j.State,
                             lastname = i.Lastname
                         }).ToArray();
               return Json(st, JsonRequestBehavior.AllowGet);
       }



在这里,如果用户在表单页面的任何字段中输入值,则必须使用if子句进行验证,并且如果它们存在于数据库中,则必须返回所有相关值
在查询中使用此If子句时遇到问题
可以请任何人帮助我
谢谢



And here if the user enters value in any of the fields in form page they must validated using if clause and if they are present in the data base all the related values must be returned
I am facing problem when am using this If clause in the query
Could any one please please help me
Thanks

推荐答案

您的if语句和查询没有多大意义.您说的是如果其中任何一个都不为空"……但是,如果提供了FirstName但其余所有都为空,该怎么办?您的查询仍将尝试在where子句中包含空值.

您可以将您的位置分为多个步骤,例如...

Your if statement and query doesn''t make much sense. You are saying ''if any of these are not null'' ... but what if say FirstName is provided but all the rest are null? Your query would still try to include the null values in the where clause.

You can break down your where into multiple steps, like so...

// Setup the main query
var query = (from i in dbContext.User_details
             join j in dbContext.User_addresses on i.Userid equals j.Userid
             join k in dbContext.Userworkdetails on j.Userid equals k.Userid
             join l in dbContext.Useredudetails on k.Userid equals l.Userid

if (!string.IsNullOrEmpty(FirstName))
     query = query.Where(u => u.FirstName == FirstName);

if (!string.IsNullOrEmpty(LastName))
     query = query.Where(u => u.LastName == LastName);

// TO DO, add in each of your possible filter options..

var results = from r in query.ToList()
              select new
                          {

                              Id = i.Userid,
                              city = j.City,
                              Firstname = i.Firstname,
                              company = k.Company,
                              college = l.College,
                              state = j.State,
                              lastname = i.Lastname
                          }).ToArray();



这样,仅使用实际提供的值来过滤结果.另外,您可以为每个可能的值创建多个重载",而不是传递空值.

就个人而言,我会创建一个"SearchCriteria"对象,并将其传递给该函数,例如



This way, only the values that are actually provided are used to filter the results. Alternatively, you could create a number of Overloads for each of the possible values, rather than passing null values.

Personally, I''d create a ''SearchCriteria'' object that I pass in to the function, e.g.

public JsonResult Details(SearchCriteria criteria)



您的条件对象将具有您可以搜索的所有可能值.



Your criteria object will have all of the possible values that you can search by.


这篇关于如何在ASP.NET MVC3中验证LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆