Excluding Condition if Parameter is null - c#

I am fetching data using LINQ and Lambda with 2 conditions using this Query. Is it possible to write this logic without if else condition -
public List<Pallet> GetPallet(string palletID, string locationID)
{
List<Pallet> data = new List<Pallet>();
if (locationID != null)
data = data.Where(x => x.PalletID == palletID && x.LocationID == locationID).ToList();
else
data = data.Where(x => x.PalletID == palletID).ToList();
return data;
}

Sure it is:
public List<Pallet> GetPallet(string palletID, string locationID)
{
List<Pallet> data = new List<Pallet>();
data = data.Where(x => x.PalletID == palletID && (locationID == null || x.LocationID == locationID)).ToList();
return data;
}

This is a way of doing it with correct results:
data = data
.Where(x => x.PalletID == palletID)
.Where(!string.IsNullOrEmpty(locationID)? x.LocationID == locationID : true)
.ToList();
NOTE: The accepted answer will return the wrong result although the syntax is correct. It'll always compares the LocationID even if the locationID is null or empty. Note that, in case of locationID == null we do not want to compare at all (we don't want this: x => x.LocationID == null).

Related

LINQ Query with method syntax

My requirement is to make boolean value (IsPC=true) only if I found any value with IsCurrent = true from the list and second condition is to filter the list with G or W codes and third condition is to check the PCBNumber length ==15 with only one from the list.
How short can i able to reduce the below query using LINQ method syntax
below is my query
var CurrentQ= p.List.Where(x => x.IsConCurrent== true);
if (CurrentQ.Count() > 0)
{
var NCurrentQwithWorQ = p.List.Where(x => x.Codes == Codes.W|| x.Codes== Codes.Q).Count();
if (NCurrentQwithWorQ != null)
{
var PCBNumber = p.List.Where(x => x.PCBNumber .Length == 15).Count();
if (PCBNumber == 1)
{
isPC = true;
}
}
}
You can use all conditions in same query like below,
var PCBNumber= p.List.Where(x => x.IsConCurrent== true && (x.Codes == Codes.W|| x.Codes== Codes.Q) && x.PCBNumber.Length == 15);
if (PCBNumber !=null && PCBNumber.Count() == 1)
{
isPC = true;
}
I'm not trying to debug what you wrote, but isn't this really what you're looking for--that is, daisy-chaining your Where conditions?
var isPC = p.List.Where(x => x.IsConCurrent == true).Where(x => x.Codes == Codes.W || x.Codes == Codes.Q).Where(x => x.PCBNumber.Length == 15).Count() == 1;
Both solutions suggested above are correct.
p.List.Where(x => x.IsConCurrent== true && (x.Codes == Codes.W|| x.Codes== Codes.Q) && x.PCBNumber.Length == 15);
p.List.Where(x => x.IsConCurrent == true).Where(x => x.Codes == Codes.W || x.Codes == Codes.Q).Where(x => x.PCBNumber.Length == 15).Count()
Actually they are performed in the same way. The Where function does not force immediate iteration through the data source. Only when you execute the Count function, LINQ will process row by row and execute criterion by criterion to find out which values should be calculated.
I can only suggest you add the Take(2) operator after the where clause. In this case LINQ will stop after finding the first two rows that matches provided criterion and other rows will not be processed.
p.List.Where(x => x.IsConCurrent == true)
.Where(x => x.Codes == Codes.W || x.Codes == Codes.Q)
.Where(x => x.PCBNumber.Length == 15)
.Take(2).Count()

Include in where statment only if not null

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.

How to handle empty fields during searching / filtering data?

I'm struggling with data searching algorithm, which has to retrieve some data from the database using multiple fields. Each textbox provides a given parameter, stored in the database, accessed by Entity Framework. It's working when I enter data to all fields but if I leave any field empty it doesn't retrieve any record.
My question is - How to handle empty fields. If I leave any field without data, it should just not consider this parameter during selecting from the database and select data basing on non-null parameters.
This is what I've created so far:
[HttpPost]
public ViewResult Search(string brand, string model, int? manufactDateMin,
int? manufactDateMax, int? priceMin, int? priceMax, int? engineCapMin,
int? engineCapMax, string engineType, int page = 1)
{
IEnumerable<Car> foundItems = repository.Cars
.Where(c => brand == null || c.Brand == brand)
.Where(c => model == null || c.Model == model)
.Where(c => manufactDateMin == null || manufactDateMax == null || (c.ManufactDate.Year >= manufactDateMin) && (c.ManufactDate.Year < manufactDateMax))
.Where(c => priceMin == null || priceMax == null || (c.Price >= priceMin) && (c.Price < priceMax))
.Where(c => engineCapMin == null || engineCapMax == null || (c.EngineCapacity >= engineCapMin) && (c.EngineCapacity < engineCapMax))
.Where(c => engineType == null || c.EngineType == engineType)
.OrderBy(c => c.Id)
.Skip(PageSize * (page - 1))
.Take(PageSize);
CarListViewModel VMmodel = new CarListViewModel
{
Cars = foundItems,
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = foundItems.Count(),
},
CarType = null
};
return View("List", VMmodel);
}
Dont use a new where statement for each comparison. combine them into one. separate them into a new line for readability. Also, your parameters are defined as strings but your comparing it to null. You will have to pass a null for this to work. If you are passing the contents of a textbox or something then the string will be "" and not a null.
.Where(c => brand == "" || c.Brand == brand) &&
(model == "" || c.Model == model) &&
((manufactDateMin == null || manufactDateMax == null || (c.ManufactDate.Year >= manufactDateMin) && (c.ManufactDate.Year < manufactDateMax)) &&
(priceMin == null || priceMax == null || (c.Price >= priceMin) && (c.Price < priceMax))) &&
(engineCapMin == null || engineCapMax == null || (c.EngineCapacity >= engineCapMin) && (c.EngineCapacity < engineCapMax))) &&
(engineType == "" || c.EngineType == engineType))
I think you should do it separately like this
IQueryable<Car> query = repository.Cars;
if (!string.IsNullOrEmpty(brand))
{
query = query.Where(x => x.Brand == brand);
}
if (!string.IsNullOrEmpty(model ))
{
query = query.Where(x => x.Model == model );
}
and after all of them you put the take and skyp, it is faster and more readable

How can I write the following lambda expression in one line?

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));

how to create dynamic linq query based on search criterias

I have a search form which i want to use to search a database for data. The searchbox has 4 checkboxes and 1 textfield. The problem is how do i build the linq query considering i dont know beforehand what textboxes the user will check for filtering the search. What i have so far is:
[HttpPost]
public ActionResult search(string ulv, string bjorn, string jerv, string gaupe)
{
var query = (from o in db.observasjonene select o);
if (ulv != null)
{
query = query.Where(o => o.art == ulv);
}
if (bjorn != null)
{
query = query.Where(o => o.art == bjorn);
}
if (jerv != null)
{
query = query.Where(o => o.art == jerv);
}
if (gaupe != null)
{
query = query.Where(o => o.art == gaupe);
}
IEnumerable ls = query.ToList();
return Json(ls, JsonRequestBehavior.AllowGet);
}
The problem with the "where" clause is that if a condition is true, it overwrites the results from the earlier condition. I guess i need an "or" statement or something..
If I have understood your question correctly, you want to check if art equals to any of provided values. You can combine those values into collection and check if collection contains art value:
var values = new [] { ulv, bjorn, jerv, game }.Where(v => v != null);
var query = from o in db.observasjonene
where values.Contains(o.art)
select o;
EF translates Contains into SQL IN operator.
I'm using two approaches in this case:
Build dynamic query:
var q = DB.Invoices.AsQueryable();
if (isPresented != null)
q = q.Where(iv => iv.IsPresented == isPresented);
if (ID != null)
q = q.Where(iv => iv.ID == ID.Value);
...........................
return from iv in q
orderby iv.DueDate descending
select iv;
Use Union to combine search results:
var q1 = db.FeeInvoice.Where(fi => [QUERY1]));
if (isPresented != null)
{
var q2 = db.FeeInvoice.Where(fi =>[QUERY2]));
q1.Union(q2);
}
if (ID != null)
{
var q3 = db.FeeInvoice.Where(fi =>[QUERY3]);
q1.Union(q3);
}
...........................
You are comparing all the parameters value to single column in the query ie. art (see you have written same column name in each where condition) . I'm not sure why are you doing so? you can simply take single parameter which compare the value like this
public ActionResult search(string value)
{
query = query.Where(o => o.art == value);
}
or if it is by mistake and you want to apply where condition along with multiple column then you can try something like this
query=query.Where(o => (o.art == ulv || ulv == string.Empty) && (o => o.bjorn == bjorn || bjorn=string.empty) && (o.jerv == jerv || jerv == string.Empty) && (o.gaupe == gaupe || gaupe == string.Empty));
Note: I assume your column name as your parameters name.

Categories

Resources