I have a generic repository using EF6. The issue has to do with association properties requiring an "Include" even though it shouldn't. The following works:
IQueryable<User> dbQuery = _db.Set<User>();
return dbQuery.Where(x => x.Child.Name == "Foo").ToList();
However, the following does not work:
Func<User, bool> filter = x => x.Child.Name == "Foo";
IQueryable<User> dbQuery = _db.Set<User>();
return dbQuery.Where(filter).ToList();
It throws an "Object Reference not set..." exception on Child.
The following resolves it:
Func<User, bool> filter = x => x.Child.Name == "Foo";
IQueryable<User> dbQuery = _db.Set<User>();
dbQuery = dbQuery.Include(x => x.Child);
return dbQuery.Where(filter).ToList();
I don't understand why this is necessary though. Anyone know a way to resolve this without using the "Include"?
You are should use Expression to let EF provider parse your query.
Change the Func<User, bool> to Expression<Func<User, bool>>
The first snippet is providing an Expression to Where, which is being translated into SQL, and is doing the entire operation in the database. The latter two are passing a compiled method to Where, which it can't translate into SQL, which means that the entire database table is being pulled down into memory, and the entire operation is run in your application. When you pull down the whole table it doesn't pull down related records unless you Include them.
The solution is not to pull down both the entire table and also all of the data from all of the related records; the solution is to do the filtering in the database rather than in your application.
Related
I need to create a generic method (ContainsLambda) that takes collection and a lambda expression (a single property) and would check if the given property contains values in the given collection.
Here is my method
public static TModel[] ContainsLambda<TModel, TKey>(IEnumerable<TKey> keys, Func<TModel, TKey> property)
{
DbSet<TModel> repository = DbContext.Set<TModel>();
return repository.Where(x => keys.Contains(property.Invoke(x)))
.ToArray();
}
Then I would call it using something like this ContainsLambda<Customer, int>(new List<int> {10, 20, 30}, p => p.Age)
The above code throws the following runtime error.
LINQ to Entities does not recognize the method 'Int32
Invoke(Customer)' method, and this method cannot be translated into a
store expression.
How can I call .Invoke() on a lambda that would be used in LINQ which would then be translated into SQL expression?
Try making your repository an enumerable. This bypasses Linq to Entities, and causes your code to run in Linq to Objects.
return repository.AsEnumerable()
.Where(x => keys.Contains(property.Invoke(x)))
.ToArray();
If you don't want all of the records in your repository to travel across the wire (i.e. you need the filtering to run on the server), you will have to write a custom SQL query to retrieve the data.
I'm running an EF statement where I need to calculate de deductibles. After long trying, I can't seem to add a custom function in a .Select() statement. Instead I'm trying to add the values after my .Select() statement.
The problem here is, in my CalculateDeductibles() I can't seem to add any values to item.Deductibles.
The GetDeductibles(item.RequestId) is a rather heavy funtion that does several extra queries, so I'm trying to prevent to convert my IQueryable to an IList object.
So there are actually 2 questions:
Can I have the GetDeductibles() function directly in my .Select() statement?
Can I somehow (with keeping an eye on performance) add the value after I did my .Select()
Code:
public IQueryable<ReinsuranceSlip> GetReinsuranceSlipsOverview(int userId, int companyId, string owner, string ownerCompany)
{
IQueryable<ReinsuranceSlip> model = null;
model = _context.Request
.Where(w => w.RequestGroup.ProgramData.MCContactId == userId)
.Select(x => new ReinsuranceSlip()
{
Id = x.Id,
RequestId = x.Id,
LocalPolicyNumber = x.LocalPolicyNumber,
BusinessLine = x.RequestGroup.ProgramData.BusinessLine.DisplayName,
BusinessLineId = x.RequestGroup.ProgramData.BusinessLine.Id,
ParentBroker = x.RequestGroup.ProgramData.Broker.Name,
LocalBroker = x.Broker.Name,
InceptionDate = x.InceptionDate,
RenewDate = x.RenewDate,
//Deductibles = CalculateDeductibles(x)
});
CalculateDeductibles(model);
return model;
}
private void CalculateDeductibles(IQueryable<ReinsuranceSlip> model)
{
//model.ForEach(m => m.Deductibles = GetDeductibles(m.RequestId));
foreach (var item in model)
{
item.Deductibles = GetDeductibles(item.RequestId);
}
}
Updated and Sorry for the first version of this answer. I didn't quite understand.
Answer 1: IQueryable is using to creating a complete SQL statement to call in SQL Server. So If you want to use IQueryable, your methods need to generate statements and return it. Your GetDetuctibles method get request Id argument but your queryable model object didn't collect any data from DB yet, and it didn't know x.Id value. Even more, your GetCarearDetuctiples get an argument so and with that argument generates a queryable object and after some calculations, it returns decimal. I mean yes you can use your methods in select statement but it's really complicated. You can use AsExpendable() LINQ method and re-write your methods return type Expression or Iqueryable.
For detailed info you should check. This:
Entity Navigation Property IQueryable cannot be translated into a store expression and this: http://www.albahari.com/nutshell/predicatebuilder.aspx
And you also should check this article to understand IQueryable interface: https://samueleresca.net/2015/03/the-difference-between-iqueryable-and-ienumerable/
Answer 2: You can use the IEnumerable interface instead IQueryable interface to achieve this. It will be easy to use in this case. You can make performance tests and improve your methods by time.
But if I were you, I'd consider using Stored Procedures for performance gain.
You'll have to understand the differences between an IEnumerable and an IQueryable.
An IEnumerable object holds everything to enumerate over the elements in the sequence that this object represents. You can ask for the first element, and once you've got it, you can repeatedly ask for the next element until there is no more next element.
An IQueryable works differently. An IQueryable holds an Expression and a Provider. The Expression is a generic description of what data should be selected. The Provider knows who has to execute the query (usually a database), and it knows how to translate the Expression into a format that the Provider understands.
There are two types of LINQ functions: the ones that return IQueryable<TResult> and the ones that return TResult. Functions form the first type do not execute the query, they will only change the expression. They use deferred execution. Functions of the second group will execute the query.
When the query must be executed, the Provider takes the Expression and tries to translate it into the format that the process that executes the query understand. If this process is a relational database management system this will usually be SQL.
This translation is the reason that you can't add your own functionality: the Expression must be translatable to SQL, and the only thing that your functions may do is call functions that will change the Expression to something that can be translated into SQL.
In fact, even entity framework does not support all LINQ functionalities. There is a list of Supported and Unsupported LINQ methods
Back to your questions
Can I have GetDeductibles directly in my query?
No you can't, unless you can make it thus simple that it will only change the Expression using only supporte LINQ methods. You'll have to write this in the format of an extension function. See extension methods demystified
Your GetDeductibles should have an IQueryable<TSource> as input, and return an IQueryable<TResult> as output:
static class QueryableExtensions
{
public static IQueryable<TResult> ToDeductibles<TSource, TResult, ...>(
this IQueryable<TSource> source,
... other input parameters, keySelectors, resultSelectors, etc)
{
IQueryable<TResult> result = source... // use only supported LINQ methods
return result;
}
}
If you really need to call other local functions, consider calling AsEnumerable just before calling the local functions. The advantage above ToList is that smart IQueryable providers, like the one in Entity Framework will not fetch all items but the items per page. So if you only need a few ones, you won't have transported all data to your local process. Make sure you throw away all data you don't need anymore before calling AsEnumerable, thus limiting the amount of transported data.
Can I somehow add the value after I did my .Select()
LINQ is meant to query data, not to change it. Before you can change the data you'll have to materialize it before changing it. In case of a database query, this means that you have a copy of the archived data, not the original. So if you make changes, you'll change the copies, not the originals.
When using entity framework, you'll have to fetch every item that you want to update / remove. Make sure you do not select values, but select the original items.
NOT:
var schoolToUpdate = schoolDbContext.Schools.Where(schoolId = 10)
.Select(school = new
{
... // you get a copy of the values: fast, but not suitable for updates
})
.FirstOrDefault();
BUT:
School schoolToUpdate = schoolDbContext.Schools.Where(schoolId = 10)
.FirstOrDefault()
Now your DbContext has the original School in its ChangeTracker. If you change the SchoolToUpdate, and call SaveChanges, your SchoolToUpdate is compared with the original School, to check if the School must be updated.
If you want, you can bypass this mechanism, by Attaching a new School directly to the ChangeTracker, or call a Stored procedure.
I'm trying to retrieve the value for a particular property of an entity into a variable using the following code.
var item = db.Notices
.Where(a => a.ID == 0)
.Select(x => x
.GetType()
.GetProperty("Spell_ID")
.GetValue(x));
I'm just playing around with this at the moment, but at some point I'd like to be able to replace the 'Spell_ID' text with any column name and get the value dynamically. Not sure if I'm going the right way around this, but I'm getting the following error:-
LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object)' method, and this method cannot be translated into a store expression.
I know I'm not doing this the right way (and I'm relatively new to C# MVC / LINQ), but I've spent so much time tinkering with the code I've lost my way...can somebody point me in the right direction please?
Your current code uses reflection to get the value of a property, but, from what I can infer from your exception message, db is an Entity Framework DbContext.
Entity framework does not support reflection at all, because your LINQ query is then converted into a SQL query by the framework itself. For this reason you have to change your approach if you really need to get a single property:
var items = db.Notices.Where(a => a.ID == 0).ToList();
var itemsProperty = items.Select(x => x.GetType().GetProperty("Spell_ID"));
This will fetch all the resources from the database and then execute the Select part in memory.
If you expect only a single entity from your database than this is a better approach:
var entity = db.Notices.SingleOrDefault(a => a.ID == 0);
var property = entity.GetType().GetProperty("Spell_ID");
Not to be tounge-in-cheek, but the error
LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object)' method, and this method cannot be translated into a store expression.
is exactly what it sounds like. LINQ is unable to translate the GetValue() method to whatever it is that Entity Framework does exactly.
While there are ways to get EF and LINQ to recognize methods, its kinda a pain. The quickest solution would be to just use a loop.
I was wondering if there's a way to use Expression class for custom queries with LINQ queries such as this
Expression<Func<TEntity, bool>> expression = (x) => x.Id = 1
var items = from item in context.TEntity
where expression
select item
I know there's a way to do this using LINQ function as the Where function takes an expression as a parameter but I really need to do this using LINQ queries
note: the code above is just an example to make you get around what I'm trying to do it's not an actual working code
note 2: I need this to work with Entity Framework
Unfortunately, Entity Framework does not natively support this type of expression projection, which is why a common attempt such as where expression.Compile().Invoke(item); will throw an exception at runtime.
However, if you use the LinqKit library (which utilizes the Expression Visitor) and call .AsExpandable() on the entity set, you can then invoke your expression dynamically:
Expression<Func<TEntity, bool>> expression = x => x.Id == 1;
var items = from item in context.Set<TEntity>.AsExpandable()
where expression.Invoke(item)
select item;
This works, at least on EF Core (I have just tested it). Don't have any EF6 project handy where I could test.
Expression<Func<TEntity, bool>> expression = (x) => x.Id = 1
var items = from item in context.TEntity
where expression.Compile()(item);
select item
Update: I just made a simple test project on EF6 and it does not work there, since it doesn't recognize the expression invocation
I have a Service Repository pattern built on top of Entity Framework.
The service has methods such as Find(IQuery query) that return IEnumerable.
The IQuery object is our own query object type where we convert strings to an IQueryable expression that the repository, which exposes IQueryable, can use.
What I'd like to do is be able to write a queryable on the client side and pass that over the service so that we can take advantage of the static typing and linq style queries instead of building our own query object in formation.
In other words I want to be able to do something like:
var query = new List<Type>().Where(x => x.Property == "argument").AsQueryable();
service.find(query);
Then I would pass the queryable or the expression it creates to my repository and work like that.
Is this sort of thing possible, or would I have to build the expression from scratch? It seems like this should be possible, but I really don't know where to begin or see examples of how to share an expression like this.
If you call AsQueryable() on an IEnumerable that doesn't already implement IQueryable, then that whole enumerable is just stuffed into a ConstantExpression, and any previous LINQ operations are not expressed as expression trees.
If you call AsQueryable() right on the source, then the LINQ operations will return an IQueryable with the proper query expression tree:
var query = new List<Type>().AsQueryable().Where(x => x.Property == "argument");