score:0
this is little tricky, but this is how we are doing it. you can combine multiple iqueryables into one as shown below.
public static iqueryable<order> activeordersquery(this mydbentities db)
{
return db.orders.where(
o=> o.active &&
(o.transactiontype == transactiontype.order ||
o.transactiontype == transactiontype.subscription )));
}
public iqueryable<userview> getview()
{
var q = context.activeordersquery();
return context.users.select( x=> new userview{
id = x.id,
active = q.any( o=> o.userid == x.id)
});
}
the only problem is you have to explicitly compare foreign keys. but this results in exactly same sql and will perform same.
this way you can refactor common queries and use them inside your views.
score:2
if you want to use your method in a lot of places, i don't think you can do anything except store it as an expression and then referring to that expression wherever you were calling the original method:
public static expression<func<user, bool>> isuseractive = user =>
user.orders.any(c => c.active &&
(c.transactiontype == transactiontype.order ||
c.transactiontype == transactiontype.subscription));
public iqueryable<userview> getview()
{
return context.users.select(p => new userview
{
id = p.id,
active = isuseractive(p)
});
}
edit: ok, so this doesn't work. you can't just "call" an expression like this. the only alternative i can think of is to turn your whole select
parameter into an expression:
public static expression<func<user, userview>> usertouserview = user =>
new userview
{
id = user.id,
active = user.orders.any(c => c.active &&
(c.transactiontype == transactiontype.order ||
c.transactiontype == transactiontype.subscription)
};
public iqueryable<userview> getview()
{
return context.users.select(usertouserview);
}
Source: stackoverflow.com
Related Query
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.DateTime AddSeconds(Double)' method, and this method cannot be translated into
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method - Entity Framework and DDD
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Double ToDouble(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.DateTime GetDate()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method ..., and this method cannot be translated into a store expression
- Additional information: LINQ to Entities does not recognize the method and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression."}
- LINQ to Entities does not recognize the method 'Char get_Chars(Int32)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Single ToSingle(System.String)' method, and this method cannot be translated into a store expression
- Converting linq select into model, LINQ to Entities does not recognize the method, and this method cannot be translated into a store expression
- Error LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method Urls.MeaningfulURL, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Boolean HasFlag(System.Enum)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method (Trying to get Previous and Next record)
- LINQ to Entities does not recognize the method
- LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'
- LINQ to Entities does not recognize the method Int32 get_Item(Int32)
More Query from same tag
- Having trouble constructing the correct Linq query syntax in c#.net for a join after join
- How do I use LINQ to return the first item from each set of Grouped data (In VB.Net)
- How do I get quoted fields from a delimited string as a list of unquoted values using LINQ?
- How to shorten this code in LINQ
- Improving performance of condtitional query inside LINQ enumerator
- Calculate the average of Lists int C# Dictionary using LINQ
- dynamic queries in dapper
- How to get sum of linq count results
- Find the equal sets of strings and their count (distinct lists)
- DataGridView & Linq
- Linq int to string
- LINQ to Entities Select taking a long time
- MVC LinQ IQueryable Anonymous type handling
- GroupBy Date from Datetime variable
- How to select distinct rows in EntitySpaces 2012
- Cannot format nullable decimal using ToString("#.##")
- LINQ .FromSQL error InvalidOperationException: Sequence contains more than one matching element
- .Include not working on Entity Framework
- How to Sort a list by field
- Sort sql data using joined dictionary
- LINQ to find array indexes of a value
- How do I search a dynamic object IEnumerable<dynamic> using a dynamically built lambda expression?
- LINQ GroupBy Month and Color Count
- How to convert a List<T> into a comma-separated list, using the class's Id property as the value
- save position from all draggable divs on save mvc
- LINQ query on datatable showing empty column
- Get all if variable is null else get matching
- Foreach loop doesn't output correctly?
- C# Remove all Items from List<T> if a property is in a List<string>
- How do I convert this SQL into LINQ?