使用linq处理空字符串

时间:2011-10-10 10:23:15

标签: c# linq linq-to-entities

我有一个linq语句,它根据表单中的用户输入搜索多个字段。只需要1个表单字段,因此我需要处理空字符串值。什么是解决这个问题的最好方法。我应该检查字符串的长度,然后使相关的变量为空,然后在我的linq语句中检查这个,或者我可以在我的linq语句中执行某些操作。我的方法如下: -

     public IEnumerable<Job> GetJobs(string jobNumber, string jobName, string projectDirectorName, string projectManagerName, string groupName) {
        return this._context.Jobs.Where(
            j => j.JobNumber.Contains(jobNumber) ||
                 j.JobName.Contains(jobName) ||
                 j.ProjectDirectorFullName.Contains(projectDirectorName) ||
                 j.GroupName.Contains(groupName));
    }

7 个答案:

答案 0 :(得分:7)

你可以用这个:

 public IEnumerable<Job> GetJobs(string jobNumber, string jobName, string projectDirectorName, string projectManagerName, string groupName) {
    IQueryable<Job> query = this._context.Jobs;

    if (!String.IsNullOrEmpty(jobNumber))
       query = query.Where(j => j.JobNumber.Contains(jobNumber));

    if (!String.IsNullOrEmpty(jobname))
       query = query.Where(j => j.JobName.Contains(jobName));

    // etc.

    return query;
}

如果这将查询数据库,那么只有在迭代此方法的结果时才会查询该数据库,而不是每个“.Where”。

答案 1 :(得分:0)

您可以尝试使用

string.IsNullOrWhiteSpace(yourString);  // .NET 4.0

OR

string.IsNullOrEmpty(yourString);

如果确实如此,则返回一个空集合。

答案 2 :(得分:0)

这样的事情怎么样:

if (!string.IsNullOrWhiteSpace(jobNumber)) return _context.Jobs.Where(j => j.JobNumber.Contains(jobNumber));
if (!string.IsNullOrWhiteSpace(jobName)) return _context.Jobs.Where(j => j.JobName.Contains(jobName));
if (!string.IsNullOrWhiteSpace(projectDirectorName)) return _context.Jobs.Where(j => j.ProjectDirectorFullName.Contains(projectDirectorName));
if (!string.IsNullOrWhiteSpace(groupName)) return _context.Jobs.Where(j => j.GroupName.Contains(groupName));
else throw new ArgumentException ("No arguments specified");

或者更好看的东西:

if (!string.IsNullOrWhiteSpace(jobNumber)) return FilterJobsByNumber(jobNumber);
if (!string.IsNullOrWhiteSpace(jobName)) return FilterJobsByName(jobName);
if (!string.IsNullOrWhiteSpace(projectDirectorName)) return FilterJobsByDirector(projectDirectorName);
if (!string.IsNullOrWhiteSpace(groupName)) return FilterJobsByGroupName(groupName);
else throw new ArgumentException ("No arguments specified");

对于适当定义的FilterJobsByNumber等。

答案 3 :(得分:0)

也许它有帮助。

    public IEnumerable<Job> GetJobs(string jobNumber, string jobName, string projectDirectorName, string projectManagerName, string groupName) {
    return this._context.Jobs.Where(
        j => (j == null || j.JobNumber.Contains(jobNumber)) ||
             (j == null || j.JobName.Contains(jobName));
}

答案 4 :(得分:-1)

应该是

String.IsNullOrWhitespaceTrim()

查看我的代码

 NorthwindDataContext db= new NorthwindDataContext();
               db.Log = sw;
               var oList = db.Categories
                   .Where(j =>
                            ( string.IsNullOrWhiteSpace(txtName.Text) || j.CategoryName.StartsWith(txtName.Text.Trim()))
                            &&
                            (string.IsNullOrWhiteSpace(txtDescription.Text) || j.Description.StartsWith(txtDescription.Text.Trim()))   
                          )
                   .Select(p => new { Name = p.CategoryName ,Description =p.Description }).ToList();  

答案 5 :(得分:-2)

我认为String.IsNullOrWhitespace检查是最好的。

答案 6 :(得分:-3)

假设您在进入此代码块之前已在搜索字词上应用了.Trim(),请按以下步骤修改代码:

public IEnumerable<Job> GetJobs(string jobNumber, string jobName, string projectDirectorName, string projectManagerName, string groupName) {
        return this._context.Jobs.Where(
            j => (j.JobNumber.Contains(jobNumber) && jobNumber!="")  ||
                 (j.JobName.Contains(jobName) && jobName != "") ||
                 (j.ProjectDirectorFullName.Contains(projectDirectorName) 
                      && projectDirectorName != "") ||
                 (j.GroupName.Contains(groupName) && groupName!=""));
    }

这里的要点是,您不需要在搜索字词上添加if条件。您可能有多个搜索字段,它可以正常工作。