How to search on the basis of all the variable parameters - c#

I am trying to write a search functionality wherein the user can search on the basis of Id, UserName and status. The data corresponding to these search filters are in different tables, so I have to put joins on these tables.
Of course user can search on the basis of Id and UserName or UserName and status and all the combinations that can be considered from these 3 filters.
What I have done is made different functions to address these combinations.
Is there a way that this can be achieved with one method using linq. I am trying to avoid the if-else and switch.
I have already looked into
Add Conditional Join Dynamically with Linq
and
LINQ - Joins in a dynamic query
This is my code where I am searching on the basis of all the parameters
public List<Application> GetApplication(int? applicationId, string userName, string status)
{
_applicationlist.Clear();
if (applicationId != 0 && !string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(status))
{
var application = (from a in _targetDbContext.DbSet<ApplicationEntity>()
join u in _targetDbContext.DbSet<UserEntity>() on a.InDraftPersonnel equals GetUserID(userName)
join v in _targetDbContext.DbSet<ApplicationVersionEntity>() on a.Id equals v.ApplicationId
join s in _targetDbContext.DbSet<ApplicationStatusEntity>() on v.VersionStatus equals s.Id
where a.Id.Equals(applicationId)
where u.UserName.Equals(userName)
where s.StatusName.Equals(status)
select (new Application
{
Id = a.Id,
AppName = a.AppName,
CreatedBy = a.CreatedBy,
CreatedOn = a.CreatedOn
})).FirstOrDefault();
_applicationlist.Add(application);
}
return _applicationlist;
}
The thing is I am not sure if the user would be searching with all the parameters of with just one.
Any help would be appreciated.

Remove if and try this:
where (!applicationid.HasValue || applicationid == 0 || a.Id.Equals(applicationId)) &&
(string.IsNullOrWhiteSpace(userName) || u.UserName.Equals(userName)) &&
(string.IsNullOrWhiteSpace(status) || s.StatusName.Equals(status))
With this where clause all parameters are optional and if one of those is null or empty those parameter don't affect query result and All combination is supported.

With
...
where a.Id.Equals(applicationId) && u.UserName.Equals(userName) && s.StatusName.Equals(status)
...
... all parameters must match
...
where a.Id.Equals(applicationId) || u.UserName.Equals(userName) || s.StatusName.Equals(status)
...
... at least one parameter must match.
...
where a.Id.Equals(applicationId) && u.UserName.Equals(userName) && s.StatusName.Equals(status)
...
is equivalent to
...
where a.Id.Equals(applicationId)
where u.UserName.Equals(userName)
where s.StatusName.Equals(status)
...
See also: Boolean logical operators (C# reference)
Note that you can construct LINQ queries pice-wise.
var query = source.Join(..) ...;
if (applicationId.HasValue) {
query = query.Where(x => x.Id == applicationId.Value);
}
if (!String.IsNullOrEmpty(userName)) {
query = query.Where(x => x.UserName == userName);
}
if (!String.IsNullOrEmpty(status)) {
query = query.Where(x => x.StatusName == status);
}
query = query.Select(x => ...);

Related

How to dynamically add or remove where clause in LINQ

Here is my LINQ query that needs to update to dynamic where clause.
If parameter's id value is 0 then where clause should return all records else where clause should match records as per id value.
public async Task<IEnumerable<dynamic>> GetFilteredRowsByID(int id)
{
return from m in _context.TableName
where m.id == (id != 0 ? id : /*AllRecordsHere*/ )
join ...
join ...
select new {...}
}
I use Asp.Net Core 2.2. Is it possible without writing another method witout where clause for this?
I will use AsQueryable lazy query instead of one LINQ Query to do it. because it's more readable i think.
var query = (from m in _context.TableName.AsQueryable() select m );
if(id != 0)
query = query.Where(w=>w.id == id);
query = ( from m in query
join ...
join ...
select new {..}
)
Try condition:
where id == 0 || m.id == id
in case when id == 0, whole expression evaluates to true, otherwise it is false and second condition m.id == id would be checked.
I'm not sure that you can achieve this cleanly with 100% expression syntax, but you can with fluent syntax:
var result = _context.TableName;
if (id != 0) result = result.Where(m => m.Id == id);
result = result.Join(...).Join(...).Select(m => m.new {...});
A key benefit of fluent syntax is that it simplifies query composition.
You can freely mix expression and fluent syntax.
Move id != 0 ? outside of the equals expression:
public async Task<IEnumerable<dynamic>> GetFilteredRowsByID(int id)
{
return from m in _context.TableName
where id != 0 ? m.id == id : true
join ...
join ...
select new {...}
}
Hopefully EF would be able to optimize the where true to remove the condition entirely.

C# linq lambda expression for OR in a list

I have this scenario in which i query with FindByMany (which takes the lambda and returns post if user country and category matches, (as seen in the "else")
But now i need to customize the return with prefered subcategories from users, so what im doing is query n times foreach subcategory and just addRange. I dont want to query 5 times the db if the user has 5 subcategories as favorite, but i dont know how to apply a dinamic OR.
So my question is, how can this code be improved for performance.
var posts = new List<Content>();
if (request.UserId != 0)
{
var user = _userRepository.FindBy(u => u.Id == request.UserId);
if (user != null && user.SubCategories.Any())
{
foreach (var temp in user.SubCategories.Select(subCategory => _contentRepository.FindManyBy(
c =>
c.Country.Id == country.Id && c.Category.Id == theCategory.Id &&
c.SubCategory.Id == subCategory.Id).ToList()))
{
posts.AddRange(temp);
}
}
}
else
{
posts = _contentRepository.FindManyBy(
c => c.Country.Id == country.Id && c.Category.Id == theCategory.Id
).ToList();
}
Could you not just materalise the sub-categories into a list, and then in your FindBy use a thatlist.Contains()?
You can get the user's sub-categories with one query and then use the list and the Contains method to filter the relevant posts. Contains method is supported by most LINQ query provides and should be translated into a single database query.
var subcategories = user.SubCategories.ToList();
foreach (var temp in _contentRepository.FindManyBy(
c =>
c.Country.Id == country.Id && c.Category.Id == theCategory.Id &&
subcategories.Contains( subCategory.Id ) ).ToList()))
{
posts.AddRange(temp);
}
You can build expression for where clause using Expression.OrElse or use enter link description here
The core of the problem is that you're forcing query execution for each item instead of dynamically building the query. #Milney has the right idea; example code below.
IEnumerable<int> subCategoryIds = user.SubCategories.Select(x => x.Id);
var posts = _contentRepository.FindByMany(c => c.Country.Id == country.Id
&& c.Category.Id == theCategory.Id
&& subCategoryIds.Contains(c.SubCategoryId)).ToList();

How to write query in Entity Framework with conditional multiple where condition? [duplicate]

This question already has answers here:
Linq: adding conditions to the where clause conditionally
(9 answers)
Closed 3 years ago.
I am creating a wcf application which is connecting to DB to get some data for customer using Entity Framework. The concept is to search a customer based on the search parameters. User can provide all or few or at least one of the search parameters. But I am quite new in Entity Framework and getting confused on how to do this. I can do this in traditional SQL coding by considering If - Else condition in c# side.
This is my code which is getting the all of the paramters:
var customers = from o in natCustomer.CustomerLists
select o;
customers = customers.Where(c => c.Name == sName && c.Age == iAge
&& c.Gender == sGender && c.Height == dHeight && c.Weight == dWeight
&& c.Nationality == sNationality
&& c.EyeColor == sEyeColor && c.SpecialMark == sSpecialMark);
Please help me by suggesting how do I get the result with few or one parameter only.
Thanks
Entity Framework queries are "deferred" queries. They don't actually run until you start asking for results. This means you can build up a query in pieces and it will (mostly) work exactly like one bigger query.
In your case, you can do something like:
var customers = from o in natCustomer.CustomerLists
select o;
if (!string.isNullOrEmpty(sName))
customers = customers.Where(c => c.Name == sName);
if (!string.isNullOrEmpty(sNationality))
customers = customers.Where(c => c.sNationality == sNationality);
if (!string.isNullOrEmpty(SpecialMark ))
customers = customers.Where(c => c.SpecialMark == SpecialMark);
etc. At the end, when you execute the customers query (for example, call ToList or use a foreach loop) EF will consolidate all of those smaller Where clauses into a single SQL query to run against your data.
Assuming you only want to find customers who match on all non-null parameters, an alternative approach is to include the null checks within the where query, and only compare the parameter to the customer data if the parameter is not null.
customers = customers.Where(c => (string.isNullOrEmpty(sName) || c.Name == sName)
&& (iAge == null || c.Age == iAge)
&& (string.isNullOrEmpty(sGender) || c.Gender == sGender));
You need some way to determine if the given inputs are set or not. For a simplification I assume you receive your parameters as nullables. So you could add an additional condition, if the parameter is provided:
customers = sName == null ? customers : customers.Where(c => c.Name == sName);
customers = iAge == null ? customers : customers.Where(c => c.Age == iAge);
customers = sGender == null ? customers : customers.Where(c => c.Gender == sGender);
...

Include("myTable") in Linq to Entities in Silverlight

I need help, I have a method to Access to my Orders table:
public IQueryable<Orders> GetOrders()
{
return this.ObjectContext.Orders.Include("UnitDetail");
}
it Works very well, and I can see it from the window Data Source, and can see Orders and UnitDetail from here
But I need make some considerations for the selected rows, so I made the next method:
public IQueryable<Orders> GetOpenOrders(string _iduni)
{
ObjectSet<Orders> orders = this.ObjectContext.Orders;
ObjectSet<Estatus> estatus = this.ObjectContext.Estatus;
ObjectSet<DetailUnit> units = this.ObjectContext.DetailsUnit;
ObjectSet<ServiceType> servicetype = this.ObjectContext.ServiceType;
var query =
(from o in orders
join e in estatus on o.ID equals e.IDOrder
join u in units on o.IDUni equals u.IDUni
join t in servicetype on u.IDType equals t.IDType
where o.IDUni.Equals(_iduni)
&& !estatus.Any(oe => (oe.Estatus == "CANCELADA" || oe.Estatus == "CERRADA")
&& oe.IDOrder == o.ID)
select o).Distinct();
return query.AsQueryable();
}
This show me the correct recs, but what happend? why I don't see UnitDetail, when I inspect the result UnitDetail is null, how I can do for put the Include clausule in this new method??
Thanks a lot in advance
Because you haven't put the Include in your new method anywhere.
EDITED: to remove the unused joins.
You should be able to just use Include as you did in your GetOrders method, and so have it as a part of your existing orders variable. You are not actually using those joins anywhere, are you intending to?
Like this:
public IQueryable<Orders> GetOpenOrders(string _iduni)
{
var query =
(from o in this.ObjectContext.Orders.Include("UnitDetail")
where o.IDUni.Equals(_iduni)
&& !this.ObjectContext.Estatus.Any(oe => (oe.Estatus == "CANCELADA" || oe.Estatus == "CERRADA")
&& oe.IDOrder == o.ID)
select o).Distinct();
return query.AsQueryable();
}

Writing the LINQ equaivalent of a SQL join

This is the SQL query I have written:
select * from Addresses a
join ProviderAddresses pa on a.address_k = pa.address_k
where pa.provider_k = 'ABC123'
and pa.active = 1
and a.active = 1
and pa.addresstype_rtk = 'HOME'
And this is the LINQ query I wrote for it:
public IQueryable<Addresses> GetAddressesesForProvider(string provider_k, string addresstype_rtk)
{
var query = from a in this.Context.Addresses
join pa in this.Context.ProviderAddresses on a.Address_K equals pa.Address_K
where pa.AddressType_RTK == addresstype_rtk
&& pa.Active == true
&& a.Active == true
select a;
return query;
}
But it is wrong. The LINQ one return thousands of records and the SQL one returns only one record.
It is IQueryable because later I need to go through its results with a for-each loop.
In the SQL I am passing hard coded values for testing but in my code for me LINQ method I also pass the same hard coded values so that is not the issue.
Looks like you may have just missed adding the provider_k condition to the where;
public IQueryable<Addresses> GetAddressesesForProvider(string provider_k, string addresstype_rtk)
{
var query = from a in this.Context.Addresses
join pa in this.Context.ProviderAddresses on a.Address_K equals pa.Address_K
where pa.Provider_K == provider_k &&
pa.AddressType_RTK == addresstype_rtk &&
pa.Active == true &&
a.Active == true
select a;
return query;
}

Categories

Resources