score:1
This should translate into an EXISTS subquery:
var tasks = from task in dc.TasksAndAccounts
where task.AccountId == accountId || (
from g in dc.AccountsInGroups
where g.AccountId == accountId && g.GroupId == task.GroupId
select g.GroupId
).Any()
select task;
score:0
Should be something like:
var tasks = from taa in TasksAndAccounts.Where(t => t.AccountId == accountId)
join aig in AccountsInGroups.Where(a => a.AccountId == accountId) on taa.GroupId equals aig.GroupId
select taa;
score:1
Ugh. Looks like you'll need a sub-select.
var ids = from task in context.TaskAndAccounts
where task.AccountId == accountId ||
(from grp in context.AccountsInGroups
where grp.AccountId == accountId
select grp.GroupId).Contains(task.GroupId)
select task;
score:1
Personally, I'd do it something like this:
var tasks = db.TasksAndAccounts.Where(x => x.AccountId == accountId);
var groups = db.AccountsInGroups.Where(x => x.AccountId == accountId);
var groupIDs = groups.Select(x => x.GroupId);
var groupTasks = db.TasksAndAccounts.Where(x => groupIDs.Contains(x.GroupId));
var allTasks = tasks.Union(groupTasks);
It's more than one line, but it's a lot clearer than trying to cram the whole thing into one line, in my opinion. Since LINQ uses deferred execution, you still won't be executing anything until you're actually using the allTasks result set.
Source: stackoverflow.com
Related Articles
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Source Code Available
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- C# Linq query help removing foreach loops creating cleaner code
- Use a linq query as microsoft local report Data Source (WinForms)
- Determine the source DataContext for a Linq to Sql query
- LINQ query returns old results when source list is re-initialized
- How to get SQL query into LINQ form in C# code
- How can I code a Linq query to do an upward Include?
- creating Linq to sqlite dbml from DbLinq source code
- Identify source of linq to sql query
- NHibernate LINQ query performance, which code fragment is better?
- Linq sub query when using a repository pattern with EF code first
- Using LINQ query result for data source for GridControl c#
- convert linq to object query to sql query (no linq to sql code or datacontext)
- A cumbersome linq query
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Converting foreach loop to LINQ query breaks code
- C# code for equivalent LINQ query
- How do I determine the source of unpredictable LINQ query results?
- How to Select top (5) contributors group by Business type code in c# linq query
- How can I code numerous MIN functions into one LINQ to DataSet query
- Avoiding repeating code with Linq query + optional params
- Unreachable expression code in LINQ query
- Reduce the line of code for this LINQ query
- source code for LINQ 101 samples
- Linq casting in a Filter design
- Error LINQ expression contains references to queries that are associated with different contexts
- Getting DataGrid current item into an object WPF
- How to use the IEqualityComparer
- Fill List<T> with IEnumerable<T> when projecting a new type using LINQ
- Group columns and add new aggregated fields using LINQ
- Adding all entries to combobox in linq to sql
- Duplicate row removal
- How to use isnull type decision in linqdatasource orderby?
- check that a specific user has specific permission in lightswitch
- Map two Identical models depending on return query
- How to perfom a mathematical operation in LINQ
- Linq Expressions on Child entities
- Access Denied when Accessing another server
- Convert xls file to xml file using c#
- Debugger Visualizer [Visual Studio 2010] - System.Linq.Expressions.Expression - not showing magnifying glass
- How to sort a list<datarow>?
- Linq to xml, when child nodes meet a certain condition
- How to convert datatable record into child custom Class
- NotMapped Field not get assigned by values on procedure calling in .NET Core