Handling empty strings using linq

I have a linq statement that searches for multiple fields based on user input from a form. Only 1 form field is required, so I need to handle empty string values. What is the best way to handle this. Should I check the length of the string and then null the corresponding vars and then check this in my linq instruction or can I do something in my linq instruction. My method is below: -

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)); } 
+6
source share
7 answers

You can use this:

  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; } 

If it queries the database, then that database will be queried only when repeating the results of this method, and not for each .Where.

+7
source

You can try using

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

OR

 string.IsNullOrEmpty(yourString); 

And if that's true, return the empty collection.

0
source

How about something like this:

 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"); 

Or something that reads better:

 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"); 

For a suitable type FilterJobsByNumber, etc.

0
source

Perhaps this may help.

  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)); } 
0
source

It should be

String.IsNullOrWhitespace and Trim()

see my code

  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(); 
-1
source

I think String.IsNullOrWhitespace best check.

-2
source

Assuming you have applied .Trim() in your search terms, before you get into this code block, change the code as follows:

 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!="")); } 

The point here is that you do not need to add if conditions to the search terms. You may have several search fields and this will work fine.

-3
source

Source: https://habr.com/ru/post/898961/


All Articles