public ActionResult sortFilteredItem(string sortByValue,string brand,string category,int price=0 )
{
var sortedData = (dynamic)null;
if (sortByValue != "")
{
if(sortByValue == "ltoh")
{
sortedData = DB.Prouducts.where(x=> x.brandName == brand & x.catName == category & x.price == price).ToList();
}
}
return View(sortedData);
}
how i can neglect if price=0 from query means that it does not make any impact on EF query because if price=0 the query does not returning expected output.Because i have not any record that has 0 price so the query is always returning null.
if(price != 0)
{
sortedData = DB.Prouducts.where(x=> x.brandName == brand & x.catName == category & x.price == price).ToList();
}
else
{
sortedData = DB.Prouducts.where(x=> x.brandName == brand & x.catName == category).ToList();
}
i have tried like this it is working good but that is lengthy process.if i have 4 or 5 more variable that,s optional so it is necessary to check null value first for working.Any recommendation ?
You can use the fllowing logic;
sortedData = DB.Prouducts.where(x=> x.brandName == brand
&& x.catName == category
&& (price == 0 || x.price == price)) //use this approach for every optional param
.ToList();
What you can do is apply filters only if the condition holds. Let's say you need to check catName and price. So:
var query = DB.Prouducts.Where(x=> x.brandName == brand);
if (category != null)
{
query = query.Where(x => x.catName == category);
}
if (price != 0)
{
query = query.Where(x => x.price == price)
}
sortedData = query.ToList();
Obviously you'll need one "if" per filter, but it is much better than considering all possible combinations.
Related
I am declaring this variable to get data from my database(example)
var products = _context.Products();
and I need to use like this example
if(ctId == -1)
{
// get project list
var products = _context.Products().where(a => a.categoryId == 2);
}
else
{
//get category list
var products = _context.Products().where(a => a.categoryId == 1);
}
but my problem how to declare var products
to using like this
if(ctId == -1)
{
// get project list
products = _context.Products().where(a => a.categoryId == 2);
}
else
{
//get category list
products = _context.Products().where(a => a.categoryId == 1);
}
To the initial problem, you would declare an IQueryable<Product> outside the if scope
IQueryable<Product> products = null;
if(ctId == -1)
products = _context.Products().where(a => a.categoryId == 2);
else
products = _context.Products().where(a => a.categoryId == 1);
However, you could also use a ternary conditional operator
The conditional operator ?:, also known as the ternary conditional
operator, evaluates a Boolean expression and returns the result of one
of the two expressions, depending on whether the Boolean expression
evaluates to true or false.
The syntax for the conditional operator is as follows:
condition ? consequent : alternative
Example
var id = ctId == -1 ? 2 : 1;
var products = _context.Products().where(a => a.categoryId == id);
or potentially
var products = _context.Products().where(a => a.categoryId == (ctId == -1 ? 2 : 1));
I am searching for a LINQ query where I have 3 parameters and two are optional parameters for this I wrote if-else conditions like below
if (id != null) { where condition}
else if (name!= null) { where condition }
else if (category != null) { where condition }
else if (id != null && name != null) { where condition }
else if (id != null && category != null) { where condition }
else if (name != null && category != null) {where condition}
else if (id != null && name != null && category != null ) { where condition }
I don't want to write more if-else conditions if there is another optional parameter added
Note. Id is not a primary key
The optimal pattern for this in EF is to add the Where conditions only conditionally. EG
IQueryable<SomeType> qry = ...;
if (id != null)
{
qry = qry.Where(x => x.Id == id);
}
if (name != null)
{
qry = qry.Where(x => x.Name == name);
}
if (category != null)
{
qry = qry.Where(x => x.Category == category);
}
var results = qry.ToList();
That way you don't clutter up the expression with lots of predicates that don't do anything, but which can mess up the query execution.
You can write it like this
id == null ? true : {id condition} &&
name == null ? true : {name condition} &&
category == null ? true: {category } and other conditions
Or
id == null || {id condition} &&
id == null || {id condition} &&
returning true will make the statement true if its value equals null.
its clean, Easy to understand and develop.
I hope it helps.
What I always do is below. Easy to read and remember.
myList
.Where(x => id == null || x.Id == id)
.Where(x => name == null || x.Name == name)
.Where(x => category == null || x.Category == category);
I have some filter parameters in the Controller of an ASP.NET MVC project and I need to create Where clause dynamically according to these parameters. If isActive parameter is true, it will get the records having the StatusId = 1. There are also userName and labId parameters in the method that should be matched in the Where clause.
public ActionResult GetStudents(int labId, string userName, bool isAll)
{
var allRecords = repository.Students;
//If isAll, get all the records having StatusId = 1
var result = allRecords.Where(m => (isAll) || m.StatusId == 1);
//???
}
I use the filter above, but I have no idea what is the most suitable way (conventions) for multiple parameters in order to fetch the result fast. Any idea?
Note: I want to filter for all of three parameters and Where clause should contain all of the combinations according to the parameter's values (also is null or empty).
var predicate = PredicateBuilder.False<Record>();
if(isAll)
predicate = predicate.AND(d => d.StatusId ==1);
predicate = predicate.AND(d => d.labID == labid && d.username = username);
return allRecords.Where(predicate);`
You can use a predicate builder
You can concatenate linq-methods as they all return an IEnumerable<T> and are combined using something like an SQL-And (dependinng on what LINQ-provider you use):
IEnumerable<Student> result = allRecords;
if(labelId.HasValue)
result = result.Where(x => x.LabelId == labelId);
else
result = result.Where(x => x.LabelId == 0); // or whatever your default-behaviour is
if(isAll)
result = result.Where(x => x.StatusId == 1);
else
result = result.Where(x => x.StatusId == 0); // or whateever your default-behaviour is when isAll is false
if(!String.IsNullOrEmpty(userName))
result = result.Where(x => x.Name == userName);
else
result = result.Where(x => x.Name == "Claus"); // or whatever the default-behaviour is when the param isnĀ“t set
Do like this
public ActionResult GetStudents(int labId, string userName, bool isAll)
{
var allRecords = repository.Students;
//If isAll, get all the records having StatusId = 1
if (isAll)
{
var result = allRecords.Where(m => m.StatusId == 1 && m.UserName == userName && m.LabId == labId);
}
else
{
// do else things
}
}
you need something like below
public ActionResult GetStudents(int labId, string userName, bool isAll)
{
var allRecords = repository.Students;
//If isAll, get all the records having StatusId = 1
if (isAll)
{
var result = allRecords.Where(m => m.StatusId == 1
&& m.LabId == labId
&& m.UserName == username);
//or
var result = from record in allRecords
where record != null &&
record.StatusId == 1
&& !string.IsNullOrWhiteSpace(record.UserName)
&& record.UserName.Equals(username)
&& record.Labid = labId
select record;
}
else
{
// do else things
}
}
How can I build the where clause dynamically, Some time the OwnerID will be zero only itemID and LocationIDwill be provided as the search criteria, in that case the LINQ should be
(from s in repository.ItemOwners.Include("OwnerDetails")
where s.ItemId == searchCriteria.ItemID &&
s.OwnerDetails.LocationId == searchCriteria.LocationID
select new { s.OwnerDetails.OwnerName, s.OwnerDetails.MobNumber }).ToList();
Some time the OwnerID and ItemId will be zero then only the LocationID will be provided as the search criteria, in that case the LINQ should be
(from s in repository.ItemOwners.Include("OwnerDetails")
where s.OwnerDetails.LocationId == searchCriteria.LocationID
select new { s.OwnerDetails.OwnerName, s.OwnerDetails.MobNumber }).ToList();
Some time the whole OwnerID, ItemID and LocationID will be provided as the search criteria, then the LINQ will be like this
(from s in repository.ItemOwners.Include("OwnerDetails")
where s.OwnerId == searchCriteria.OwnerID &&
s.ItemId == searchCriteria.ItemID &&
s.OwnerDetails.LocationId == searchCriteria.LocationID
select new { s.OwnerDetails.OwnerName, s.OwnerDetails.MobNumber }).ToList();
Here only the where clause is changing, Please help me to solve. How I can I build the where clause dynamically (Please note, here I am having a navigation property which is OwnerDetails.LocationId).
You can easily do it by using method-based query. You can add the conditions one at a time and call Select and ToList at the end:
// Where(x => true) might not be necessary, you can try skipping it.
var query = repository.ItemOwners.Include("OwnerDetails").Where(x => true);
if (searchCriteria.OwnerID != null)
query = query.Where(s => s.OwnerID == searchCriteria.OwnerID);
if (searchCriteria.ItemID != null)
query = query.Where(s => s.ItemID == searchCriteria.ItemID);
if (searchCriteria.OwnerID != null)
query = query.Where(s => s..OwnerDetails.LocationId == searchCriteria.LocationID);
var results = query.Select(s => new { s.OwnerDetails.OwnerName, s.OwnerDetails.MobNumber }).ToList();
Simplest is just check the zero condition in the Where clause:
(from s in repository.ItemOwners.Include("OwnerDetails")
where (searchCriteria.OwnerID == 0 || s.OwnerId == searchCriteria.OwnerID) &&
(searchCriteria.ItemID == 0 || s.ItemId == searchCriteria.ItemID) &&
s.OwnerDetails.LocationId == searchCriteria.LocationID
select new { s.OwnerDetails.OwnerName, s.OwnerDetails.MobNumber }).ToList();
I'm trying to pass a null value from a RenderAction to another view. But in between, at the controller, my linq lambda expression is not loading the right field, despite the null value going through correctly..
SprintManager.cshtml
<div id="Global_Backlog_Board" class="Board_Panel">
#{Html.RenderAction("ListOfSingleCards", new
{
State_ID = 1
});}
</div>
HomeController.cs
public PartialViewResult ListOfSingleCards( int? Sprint_ID,
int State_ID = 1)
{
var Cards = db.Cards.Where(x => x.State_ID == State_ID &&
x.Sprint_ID == Sprint_ID &&
x.Deleted != 1 &&
x.Archive != 1).ToList();
return PartialView(Cards);
}
So Sprint_ID is being passed over and loaded as null here, but I can't get the query to load the rows correctly.
In fact, the following works:
var Cards = db.Cards.Where(x => x.State_ID == State_ID &&
x.Sprint_ID == null &&
x.Deleted != 1 &&
x.Archive != 1).ToList();
So I suppose I could check if Sprint_ID is null and depending on the result run one of the two seperate queries, but I'd like to understand why my original attempt is not working.
Thank you!
I don't know the correct answer but based on your solution you should be able to tidy it up:
var cards = new List<Card>();
var query = db.Cards.Where(x => x.State_ID == State_ID &&
x.Deleted != 1 &&
x.Archive != 1);
if (Sprint_ID.HasValue)
query = query.Where(x => x.Sprint_ID == Sprint_ID);
else
query = query.Where(x => x.Sprint_ID == null);
cards = query.ToList();
A nullable int won't return "null" in the way that you're thinking. You have to check the HasValue property of it to determine if there is a value, and if so then use it otherwise use null:
public PartialViewResult ListOfSingleCards( int? Sprint_ID,
int State_ID = 1)
{
var Cards = db.Cards.Where(x => x.State_ID == State_ID &&
x.Sprint_ID == Sprint_ID.HasValue ? Sprint_ID.Value : null &&
x.Deleted != 1 &&
x.Archive != 1).ToList();
return PartialView(Cards);
}
Until something better comes a long, I'm using this:
var Cards = new List<Card>();
if (Sprint_ID == null)
{
Cards = db.Cards.Where(x => x.State_ID == State_ID &&
x.Sprint_ID == null &&
x.Deleted != 1 &&
x.Archive != 1).ToList();
}
else
{
Cards = db.Cards.Where(x => x.State_ID == State_ID &&
x.Sprint_ID == Sprint_ID &&
x.Deleted != 1 &&
x.Archive != 1).ToList();
}