i have a query in SQL
Declare @Id Int
set @UserName = Null
set @FullName = Null
Select * from tblPermission where (UserName= @UserName OR @UserName IS NULL) && (
FullName = @FullName OR @FullName IS NULL)
i m using Fluent nHibernate as ORM .
i m trying this:
var Allusers = from u in session.Query<User>()
where u.UserName.Contains(UserName) || UserName == null
&& u.FullName.Contains(FullName) || FullName == null
select u;
This linq query works fine for UserName but not working if UserName and FullName filters both have some value.
how to achieve this functionality in LINQ ? Any Idea?
Thanks
I would go with dynamically building the query:
Since it’s a query, it won’t be executed until you enumerate it so chaining
Wherecalls won’t execute it multiple times. Plus you’ll get a SQL query optimized for each case.