c# - Linq with null parameters not working -
i don't understand why query not working, can please me?
public static ienumerable<selectlistitem> mylist(int? id, string name="") { var list =db.entity .where(p=> (name==null? p.name !=null : p.name==name) && (id.hasvalue || p.id==id) .select(n=>new selectlistitem() { text=n.name, value=n.id.tostring() }).tolist(); return list; }
i want have full list when both parameters null!! empty list when both parameters null.
the snippet code big method contain several query one.
if understand correctly not want perform filtering when value null
. should write:
.where(p=> (name == null || p.name == name) && (id == null || p.id == id)
and should change signature of function set default value parameter name
null
rather empty string.
public static ienumerable<selectlistitem> mylist(int? id, string name = null)
Comments
Post a Comment