Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, it's not entirely clear what you want, but if you're trying to only add where clauses for the parameters which are non-null, you could do:</p> <pre><code>public IQueryable&lt;Student&gt; FindByAllStudents (int? id, string name, int? courseID, bool? isActive) { IQueryable&lt;Student&gt; query = db.Student; if (id != null) { query = query.Where(student =&gt; student.ID == id.Value); } if (name != null) { query = query.Where(student =&gt; student.Name.Contains(name)); } if (courseID != null) { query = query.Where(student =&gt; student.CourseID == courseID.Value); } if (isActive != null) { query = query.Where(student =&gt; student.IsActive == isActive.Value); } return query; } </code></pre> <p>I haven't tried that, and it's <em>possible</em> that LINQ to SQL would get confused by the code to find the value of the nullable value types. You may need to write code like this:</p> <pre><code> if (courseID != null) { int queryCourseID = courseID.Value; query = query.Where(student =&gt; student.CourseID == queryCourseID); } </code></pre> <p>It's worth trying the simpler form first though :)</p> <p>Of course, all this gets a bit irritating. A helpful extension method could make life more concise:</p> <pre><code>public static IQueryable&lt;TSource&gt; OptionalWhere&lt;TSource, TParameter&gt; (IQueryable&lt;TSource&gt; source, TParameter? parameter, Func&lt;TParameter, Expression&lt;Func&lt;TSource,bool&gt;&gt;&gt; whereClause) where TParameter : struct { IQueryable&lt;TSource&gt; ret = source; if (parameter != null) { ret = ret.Where(whereClause(parameter.Value)); } return ret; } </code></pre> <p>You'd then use it like this:</p> <pre><code>public IQueryable&lt;Student&gt; FindByAllStudents (int? id, string name, int? courseID, bool? isActive) { IQueryable&lt;Student&gt; query = db.Student .OptionalWhere(id, x =&gt; (student =&gt; student.ID == x)) .OptionalWhere(courseID, x =&gt; (student =&gt; student.CourseID == x)) .OptionalWhere(isActive, x =&gt; (student =&gt; student.IsActive == x)); if (name != null) { query = query.Where(student =&gt; student.Name.Contains(name)); } return query; } </code></pre> <p>Using a higher order function like this could get confusing if you're not really comfortable with it though, so if you're not doing very many queries like this you might want to stick with the longer but simpler code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload