score:2
You can use compiled query in other query but you can't make it dependent on the outer query. Examples
// You can do
var someParams = 10;
var dataQuery = from x in ctx.SomeData
join y in myCompiledQuery.Invoke(ctx, someParams)
on x.Id equals y.Id
where x.Name = "ABC"
select new { x, y };
// You can't do - this example will not compile but let's use it for description
var dataQuery = from x in ctx.SomeData
join y in myCompiledQuery.Invoke(ctx, x.SomeParams)
on x.Id equals y.Id
where x.Name = "ABC"
select new { x, y };
The difference is that first example just executes delegate (compiled query is a delegate) and returns IQueryable
. The second example can't execute delegate because it is dependent on outer query data so it takes it as something that must be added to expression tree and eveluated during query execution. This fails because EF provider is not able to translate delegate invocation.
score:0
You can combine and embed queries, but I do not think you can use compiled queries. It really shouldn't matter much though, because EF will only compile the combined query once and then cache it (and the database backend should cache the associated query plan).
You could therefore use something along these lines:
var reviewQuery = from mcq in reviews
select new
{
prod_id = mcq.prod_id
rating = mcq.review_rating,
review = mcq.review_text
};
from cat in ctx.cat_table
join prod in ctx.prod_table on cat.category_id equals prod.category_id
select new
{
cat_id = cat.category_id,
prod_id = prod.product_id,
name = prod.product_name,
descript = prod.product_description,
price = prod.price,
reviews = from r in reviewQuery where r.prod_id == prod_id select r
}
Source: stackoverflow.com
Related Articles
- Can I use a compiled query as a source in a second query?
- Can I cite a Compiled Query as the Data Source to a Compiled Query?
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Entity Framework 6 Compiled LINQ Query
- LINQ-to-SQL Compiled Query Problem (works as uncompiled query)
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- linq join query get single record from second table
- Compiled LINQ query & DataLoadOptions... with a twist!
- LINQ Source Code Available
- prevent unnecessary cross joins in count query of generated sql code
- Linq Compiled Query using Contains (Like SQLs IN statement)
- .NET 4 Code Contracts: "requires unproven: source != null"
- EF Code First comparing null values generates strange query
- 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?
- LINQ How to combine second item in Tuple after query using where on first item
- Entity Framework Compiled Query
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- Avoid extra loop and could not find implementation of query pattern for source type int Select not found
- Exception in a CRM LINQ query with joins. Attribute in second table doesn't exist
- 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
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- LINQ query returns old results when source list is re-initialized
- How to query by where clause with EF code first
- How do I make an EF-Core (2.1) Compiled Query return an IQueryable?
- How to union two LINQ queries but the second query need have more fields
- How to get SQL query into LINQ form in C# code
- How to count total of specific column in a table
- LinQ ORM Data Objects and Inheritance
- LINQ grouping/subquery to fill a hierarchy data strcuture
- How to find duplicate dates in a list and group the description
- Converting Data in a C# List from one type to another.
- Group By object within a nested list linq
- How to parameterize a selector with a function in EF query?
- How to set to value to 0 if max field is empty
- Mapping Linq Group By Results To An Object
- Linq, how to delete items in the middle of a query return?
- Is there another way to take N at a time than a for loop?
- How to create an Expression that invokes (or is combined with) another Expression using a closure object as argument?
- linq xml error handling
- C# Linq on a list of dictionaries to filter it
- How to convert list of arrays into a multidimensional array
- MVC Strongly typed IQueryable<IGrouping<TKey, TElement>> model
- How to get duplicate items from a list using LINQ?
- Read text data from file using LINQ
- LINQ to XML row with two attributes
- LINQ result is not showing as expected