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;
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;
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;
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.
- 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
More Query from same tag
- The Type of one of the Expression in the join clause is incorrect.Type inference failed in the call to join
- Update a Linq to XML Element with an Element
- LINQ result in DataRow
- Filter anonymous object list by executing string clause in where condition
- Populate a list of Objects (With Linq?)
- LINQ Pad- Can you do nested projections? Limiting selected columns while maintaining heirachical grid output style
- How to filter to all variants of a generic type using OfType<>
- Linq and grouping with three tables
- How to get data from a Table if they exist in another Table C# LINQ
- Merge 2 arrays using LINQ
- Linq: how to use specifications against associated objects
- How to handle IEnumerable with IGrouping in the view?
- Whats the best solution to Entity Framework cores lack of moderate LINQ query support?
- How to validate null values on linq
- How to remove items with count == 3 using LINQ
- linq C# why the value change
- Using String.Split().Contains() on CSV column using Linq/Lambda
- How to merge two classes into single unit?
- Error in setting LinqDataSource Where Clause with datetime column
- Order of Execution of Condition in a Where clause
- Give a number to return the approximated value of an Enum?
- Grouping by month with LINQ
- What determines what the Where in a Linq statement is able to parse?
- Reusable LINQ query except for where clause
- Updating object Linq'd from collection doesn't update the object in collection
- Filter generic list with generic dictionary
- How can I select specific elements from an XML using Linq to Xml?
- How to make pyramid shape asp.net datalist?
- update a List<Object> with LINQ
- How do I fix my linq query