score:2
Maybe you are looking for this.
Filtere where
x.Alert.ShowOldAlerts == true
or
x.Alert.ShowOldAlerts == false && Read == false
Example
var results = _context.GeneratedAlerts
.Include(a => a.Alert)
.Where(x => x.Alert.ShowOldAlerts || !x.Read)
.Select(a => new GeneratedAlertResponse
{
Id = a.Id,
Alert = _mapper.Map<AlertInfoResponse>(a.Alert),
ReceivedDate = a.ReceivedDate,
Completed = a.Completed
...
}.ToList();
If you need the results separated, it's probably best to still keep the one query (one round trip to the database), however you can filter the results after the fact.
var alerts = results.Where(x => x.Alert.ShowOldAlerts);
var read = results.Axcept(alerts );
score:0
For the first case which retrieves generated alerts if Read
is true and ShowOldAlerts
is false. If I understood the question correctly, this should work.
var alerts = _context.GeneratedAlerts
.Where(c => c.Read && !c.Alert.ShowOldAlerts);
For the second case where you only retrieve the generated alerts only if ShowOldAlerts
is false.
var alerts = _context.GeneratedAlerts
.Where(c => c.Alert.ShowOldAlerts);
To return as one list, the conditions seem awkward, but here it is
var alerts = _context.GeneratedAlerts
.Where(c => (c.Read && !c.Alert.ShowOldAlerts) || c.Alert.ShowOldAlerts);
Source: stackoverflow.com
Related Articles
- Using linq c# how can I select using a condition two different lists
- select object which matches with my condition using linq
- Merge 2 Lists of different types using LinQ
- How to efficiently select certain items from two lists using LINQ
- Linq to compare 2 different lists and select the outer join
- Find all items from two different custom lists using LINQ (C#)
- C# Combining 2 lists of different types using Linq [Method syntax]
- How to select and count base on a condition of another field using dynamic linq core
- Entity Framework Linq to Sql: select from 2 different subqueries based on boolean condition
- How to merge multiple lists based on a condition using LINQ
- How to write C# LINQ code to select based on condition
- Select a sub List<T> with filtering condition List<int> using Linq
- Using linq to select an object that has a min Date after a Where condition
- Match values in two different lists using Linq
- Select columns with specific condition using Linq to SQL
- Difference between numbers present in two different lists based on another property using LINQ
- Finding and printing a value from 2 different lists using LINQ
- LINQ select with more than one condition using OR
- Return select objects and only desired subvalues from a list of lists using LINQ
- Totalling inner lists by different methods on a list using Linq
- Merging lists of objects using linq while showing preference over a condition in one list
- Select from Multiple List inside cascading DropDownList using condition in Linq query c#
- C# Create matrix using 3 different lists - using LINQ
- get list from xml using linq based on true if there is no true then select false condition tag element in c#
- Convert string[] to int[] in one line of code using LINQ
- Select distinct using linq
- LINQ Using Max() to select a single row
- Linq code to select one item
- How to select values within a provided index range from a List using LINQ
- Return list using select new in LINQ
- Convert Sum(case) SQL statement to LINQ query in Entity Framework
- CSV to object model mapping
- Linq query to select only common items in two lists
- Selecting array values from specific indexes order by index array
- Database is locked when inside a foreach with linq without ToList()
- NHibernate 3.0 and LINQ: what am I missing?
- SQL Server foreign key ID and entity property
- Select method is not called using LinQ query syntax
- Getting null when concatenating string in linq
- How to get all columns' max, min, mean values
- How to return single value from anonymous Linq.Expressions.Expression
- LINQ to Entities - method cannot be translated into a store expression
- search object list by computed property
- Can't use custom extension methods with IronPython
- How to make aggregate function in single method using linq?
- How to select all from datatable under some condition?
- Does LINQ to SQL support the t-sql "in" statement
- Converting SQL containing top, count, group and order to LINQ (2 Entities)
- LINQ causing my obfuscator to break
- Error when using a Linq Expression variable instead of lambda expression directly