I want to do a query with lambda expression.
My database is a Cosmos DB.
I want to filter for two parameters and one of the two can be null.
For example i want to search for name and lastname and one of both is null.
This is that I am trying:
var result = this._client.CreateDocumentQuery<Person>(
UriFactory.CreateDocumentCollectionUri(_idDatabase, _idCollection), queryOptions)
.Where((f) => f.Name == Name && f.LastName == lastName )
.AsEnumerable()
.ToList();
So this?
var result = this._client.CreateDocumentQuery<Person>(
UriFactory.CreateDocumentCollectionUri(_idDatabase, _idCollection), queryOptions)
.Where((f) => (f.Name == Name || (f.Name == null && f.LastName != null)) && (f.LastName == lastName || (f.LastName == null && f.Name != null))
.AsEnumerable()
.ToList();
try something like this:
var result = this._client.CreateDocumentQuery<Person>(
UriFactory.CreateDocumentCollectionUri(_idDatabase, _idCollection), queryOptions)
.Where(f => f.Name == $"{Name ?? f.Name}") &&
f.LastName == $"{lastName ?? f.LastName}") )
.AsEnumerable()
.ToList();
Maybe you can work with IQueryable:
IQueryable<Person> iPerson = this._client.CreateDocumentQuery<Person>(
UriFactory.CreateDocumentCollectionUri(_idDatabase, _idCollection), queryOptions);
if(Name != null) iPerson = iPerson.Where(f => f.Name == Name);
if(lastName != null) iPerson = iPerson.Where(f => f.LastName == lastName)
return iPerson.AsEnumerable().ToList();
Related
In the following GetByExpression, sometimes the r.branchid is null. How do I add a conditional in there so it utilises a different field (r.Invoice.Branch.Id) to check against when r.branchid is null?
var t = receiptRepo
.GetByExpression(r => r.BranchId == branchId)
.Where(r => r.Date == selectedDate && r.BatchId == null)
.ToList();
Use a ternary operation
(r.BranchId != null? r.BranchId:r.Invoice.Branch.Id ) == branchId //or whatever you want to compare
You can try using null-coalescing operator ??:
r => (r.BranchId ?? r.Invoice.BranchId) == branchId
Here if r.BranchId is null we use r.Invoice.BranchId to compare with branchId. Note, that we can chain ?? operators, e.g.
r => (r.BranchId ?? r.Invoice.BranchId ?? r.Bill.BranchId) == branchId
we try BranchId if it's null, we have a look at r.Invoice.BranchId and if it's null we get r.Bill.BranchId etc.
Your Linq query can be
var t = receiptRepo
.GetByExpression(r => (r.BranchId ?? r.Invoice.BranchId) == branchId)
.Where(r => r.Date == selectedDate && r.BatchId == null)
.ToList();
You can do this:
var t = receiptRepo
.GetByExpression(r => (r?.BranchId ?? r.Invoice.BranchId) == branchId)
.Where(r => r.Date == selectedDate && r.BatchId == null)
.ToList();
I have the following :
how can i include a filter in my where statement only if it's not null?
i tried the following but i always get 0 records
public async Task<IList<UserRefDto>> GetUserRef(Dictionary<string, string> filter) {
var iod = 549; // a test user who has 2548 records in db
var favorites = filter.ContainsKey("fav") ? filter["fav"].ConvertToGuid(",") : null;
var histories = filter.ContainsKey("his") ? filter["his"].ConvertToGuid(",") : null;
var orders = filter.ContainsKey("ord") ? filter["ord"].ConvertToGuid(",") : null;
var result = await context.UserRefernces
.Where(x =>
x.User_iod = iod
&& (favorites != null && favorites.Contains((Guid)x.FavId))
&& (histories != null && histories.Contains((Guid)x.HistoryId))
&& (orders != null && orders.Contains((Guid)x.OrderId))
)
.AsNoTracking()
.ToListAsync()
.ConfigureAwait(false);
return result;
}
EDIT
Note: If all keys have values then i got records back, only if one or more is null i got nothing back from db
You can build your query with several .Where() statements, which will be added via an AND condition. But add the additional .Where() statements only when the values are present.
var query = context.UserRefernces
.Where(x => x.User_iod == iod);
if (favorites != null) {
query = query.Where(x => favorites.Contains((Guid)x.FavId));
}
// same for "histories" and "orders"
var result = await query
.AsNoTracking()
.ToListAsync()
.ConfigureAwait(false);
Replace favorites != null && with favorites == null || in your query as follows:
var result = await context.UserRefernces
.Where(x => x.User_iod = iod
&& (favorites == null || favorites.Contains((Guid)x.FavId))
&& (histories == null || histories.Contains((Guid)x.HistoryId))
&& (orders == null || orders.Contains((Guid)x.OrderId)))
.AsNoTracking()
.ToListAsync()
.ConfigureAwait(false);
return result;
Now the query will work as expected.
Using this Linq code I get any values corresponding to the search input i.e if I search country = Italy and gender = female I get both employees from Italy and employees who are female but I need it to be more specific.
i.e if I search Country = Italy and Gender = female I need to get female employees from Italy. Please suggest me a Linq code for the same
Also, I have five search inputs (First Name, Last Name, Designation, Country, Gender) so just (&&) only doesn't do the work here!
Here's the code:
List<Employee> Elist = userdb.Employees
.Where(i => i.FirstName == Fn ||
i.LastName == Ln ||
i.Designation == desig ||
i.Country == country ||
i.Gender == gender)
.ToList();
This is a situation where the nature of IQueryable comes in very useful. You can add Where clauses to your query without actually executing anything against the database. The SQL would only be executed when you materialise the data, for example using ToList(). This is called deferred query execution.
So you can write your code like this:
IQueryable<Employee> query = userdb.Employees;
if(!string.IsNullOrEmpty(Fn))
{
query = query.Where(e => e.FirstName == Fn);
}
if(!string.IsNullOrEmpty(Ln))
{
query = query.Where(e => e.LastName == Ln);
}
// etc. etc.
List<Employee> Elist = query.ToList();
Most likely, you are not wanting to include criteria that is not filled in. You would only want to filter by a value if the value exists (or is not null). Use an IQueryable to build your search and then assign it to Elist.
IQueryable<Employee> Query = userdb.Employees;
if (Fn != null) {
Query = Query.Where(i => i.FirstName.Equals(Fn));
}
if (Ln != null) {
Query = Query.Where(i => i.LastName.Equals(Ln));
}
if (desig != null) {
Query = Query.Where(i => i.Designation.Equals(desig));
}
if (country != null) {
Query = Query.Where(i => i.Country.Equals(country));
}
if (gender != null) {
Query = Query.Where(i => i.Gender.Equals(gender));
}
List<Employee> Elist = Query.ToList();
Personally I would use a PredicateBuilder here. A small example, let's say you just have 2 queries:
Expression<Func<Person, bool>> hasFirstName = p1 => p1.FirstName == Fn;
Expression<Func<Person, bool>> hasLastName= p2 => p2.LastName == Ln";
You could build this into a predicate builder like so and keep on expanding using any sort of logic:
var predicate = PredicateBuilder.False<Employee>();
if (!string.IsNullOrEmpty(Fn))
{
predicate = predicate.And(e => e.FirstName == Fn);
}
if (!string.IsNullOrEmpty(Ln))
{
predicate = predicate.And(e => e.FirstName == Ln);
}
var result = userdb.Employees.Where(predicate);
Try this
List<Employee> Elist = userdb.Employees
.Where(i => (Fn == null || i.FirstName == Fn ) &&
(Ln == null || i.LastName == Ln ) &&
(desig == null || i.Designation == desig) &&
(country == null || i.Country == country) &&
(gender == null || i.Gender == gender)
.ToList();
I'm trying to write a query to select data from database. I have the following code :
from notes in ctx.Notes
.Where(x => x.UserId== user.UserId
|| x.UserId == user.FamilyId
|| x.UserId == user.CompanyId).DefaultIfEmpty()
The problem with this is that the FamilyId and CompanyId are both nullable types and may not have any value at all which corrupts the whole query. How can I rewrite it so it only looks for FamilyId/CompanyId if they have values?
Create condition query:
var users = ctx.Notes.Where(x => x.UserId == user.UserId);
if (user.FamilyId != null)
{
users = users.Union(ctx.Notes.Where(x => x.UserId == user.FamilyId));
}
if (user.CompanyId != null)
{
users = users.Union(ctx.Notes.Where(x => x.UserId == user.CompanyId ));
}
var result = users.ToArray();
Simple, just add an AND clause to check if it's not null:
from notes in ctx.Notes.Where(x => x.UserId== user.UserId || (user.FamilyId ! =null && x.UserId == user.FamilyId) || (user.CompanyId !=null && x.UserId == user.CompanyId)).DefaultIfEmpty()
I want to fetch the records as follows
SearchResult.condition is null then fetch all the rows from Person
if SearchResult.condition is false then fetch the rows where PersonType column contains null value
if SearchResult.condition is true then fetch the rows where PersonType column contains non null value
struct SearchResult
{
public string Name;
public bool? condition;
}
Expression<Func<Person, bool>> expression;
if(condition==null)
{
expression= (a =>
(SearchResult.Name==null || a.Name == SearchResult.Name)
);
}
else if(condition.Value == true)
{
expression= (a =>
(SearchResult.Name==null || a.Name == SearchResult.Name)
&& a.PersonType != null)
}
else if(condition.Value == false)
{
expression= (a =>
(SearchResult.Name==null || a.Name == SearchResult.Name)
&& a.PersonType == null)
}
I want to write the expression in one expression instead of using if else conditions. Can u plz help me in it?
Well you can do it with a conditional operator, but you need to specify the type of the expression tree for each lambda expression:
var expression = condition == null
? (Expression<Func<Person, bool>>) a => SearchResult.Name == null ||
a.Name == SearchResult.Name
: condition.Value
? (Expression<Func<Person, bool>>) a => (SearchResult.Name == null ||
a.Name == SearchResult.Name) &&
a.PersonType != null
: (Expression<Func<Person, bool>>) a => (SearchResult.Name == null ||
a.Name == SearchResult.Name) &&
a.PersonType == null;
But assuming you're going to use this with a LINQ query, you'd be much better off with something like:
var query = foo.Where(a => SearchResult.Name == null ||
a.Name == SearchResult.Name);
if (condition != null)
{
query = condition.Value ? query.Where(a => a.PersonType != null)
: query.Where(a => a.PersonType == null);
}
As an aside, I'd strongly advise you to avoid writing mutable structs or using public fields.
You could shorten as:
expression = a =>
(SearchResult.Name == null || a.Name == SearchResult.Name) &&
(SearchResult.condition == null || Search.condition == (a.PersonType != null));