Linq to entities custom function in select - linq

In Linq to Entities one can't use standard c# method to modify the results in the "select" clause, canonical functions are required.
I need to invoke such query:
CoreEntities.Contracts.Select(
c=> new {
c.Name,
c.Type,
Role = MapToRole(c));
private string MapToRole(Contract contract) {
switch(contract.Type) {
case: CTypes.Main: return "somerole1";break;
case: CTypes.Secondary: return "somerole2";break;
// ...
default: break;
}
return "none";
}
The "MapToRole" is a C# method created just to declutter the linq query.
Is there a way to create a custom c# function that would be accepted by Entity Framework "linq to Entity" parser?
I've found a solution for query filters but not for data formatting.

It appears that as it's a simple transformation, there's no reason this needs to be translated by the provider. I would suggest simply adding AsEnumerable() before your Select() to "detach" your projection from the provider.
Alternatively you could have a CTypes property in your data type and a method/property that performs the transformation of that within your model.
As a side note, doing this particular transformation in your application layer means that you're only pulling through the enum value, not a string - therefore less data from the provider.

Related

Implementing service queryables: IQueryable to IEnumerable and back

In RIA Services (and probably other frameworks such as WCF Data Services) one can implement data services methods by supplying methods returning an IQuerably. For example:
IQueryable<Customer> GetCustomers()
{
return this.DbContext.Customers.Where(c => !c.IsDeleted);
}
A query from the client can then supply an additional filter (among other things). Both the client-provided filter and the filter for returning only undeleted customers will be combined an sent to the database in SQL by entity framework (or whatever ORM is used).
Often entity framework isn't powerful enough to express the query one wants to write, in which case we have to go to linq to objects:
IQueryable<CustomerWithFluff> GetCustomers()
{
var customers =
this.DbContext.Customers
.Include("FluffBits")
.Where(c => !c.IsDeleted)
.ToList();
return
from c in customers
select new CustomerWithFluff()
{
CustomerName = c.Name,
ComplexFluff = String.Join(", ", from b in c.FluffBits select b.FluffText)
};
}
The sql to the database will now still contain the restriction on "IsDeleted", but not any further filter provided by a client: Those will be applied on the linq to objects level after all the data has already been fetched.
If I don't care to let the client filter on any of the data that I need to compose with linq-to-objects (only "ComplexFluff" in this example), is there a way to still allow filtering on the properties that are simply projected (only "CustomerName" in this example)?
Yes there is, however is not as simple as one can expect. You have to override the
public override IEnumerable Query(QueryDescription queryDescription, out IEnumerable<ValidationResult> validationErrors, out int totalCount)
method, where you can get, in queryDescription.Query the actual query and Expression that is going to be executed against your Queryable (that is, what is being returned by queryDescription.Method.Invoke(this, queryDescription.ParamterValues)).
You can then get the expression sent by the client and pass it to your method (maybe as a reference to some sort of field in you domainService, don't forget that WCF Ria Services are instantianted at each call, i.e. like a regular wcf perCall instancing model) where you'll have to combine
var customers =
this.DbContext.Customers
.Include("FluffBits")
.Where(c => !c.IsDeleted)
prior to the "ToList()" method call.
Not easy, but quite possible

AutoMapper Project().To() not working for interface destination

I have a situation in AutoMapper where I need to create a mapping with an interface destination. This is not a problem, and when using the normal Mapper.Map, it returns a proxy class as expected. However, when I try to do something similar with .Project().To(), which I need to use because an ORM is involved, I have issues. Here is the exact code that is failing which I replicated in a unit test:
AutoMapper.Mapper.CreateMap<RoleDto, IRole>(); //both just have Id/Name
IQueryable<RoleDto> roleDtos = new List<RoleDto>
{
new RoleDto { Id = 1, Name = "Role1" },
new RoleDto { Id = 2, Name = "Role2" }
}.AsQueryable();
//this works:
List<IRole> roles = roleDtos.Select(
roleDto => AutoMapper.Mapper.Map<IRole>(roleDto)
).ToList();
//this does not work:
List<IRole> roles2 = roleDtos.Project().To<IRole>().ToList();
I'm getting ArgumentException:
ArgumentException: Type 'UnitTestProject5.IRole' does not have a default constructor
In my real implementation the .Select is being performed on an Entity Framework collection, which is why I need to stick with .Project().To().
I have no issues with .Project().To() if the destination is not an interface. Additionally, I have no issues with the interface destination if I use .Map().
How can I get the interface destination and .Project.To() to work at the same time? Why is .Project.To() not giving me proxy classes like .Map() is? Any ideas?
Thanks!
Mapper.Map() takes the linq-to-objects route to materialize objects. As you said, AutoMapper is capable of creating types on the fly if the mapped target is an interface.
Project().To() is a way to translate the whole query, including the mapping, into SQL. Which is great, because only the properties that are required for the target object are included in the SQL query. However, the things AutoMapper does for creating types on the fly (undoubtedly some Refection voodoo) can never be part of an expression tree that can be converted into SQL. That's why Project.To simply tries to new up an object, even if it's an interface.
You'll have to use a concrete type as a mapping target. Of course, this type can implement an interface, so you can keep the independence you want.

Intercepting context.SaveChanges() in MVC 3 so that I can modify and use a custom sql query instead

I am working on an MVC3 project whose model is designed using Code First approach. I am using EF4 for ORM and have a need where I need two thing -
1. Intercept the context.SaveChages method
2.Do my own custom update query for one specific entity type only.
I got the first part working by overriding the SaveChanges() method like -
public override int SaveChanges()
{
var modifiedItems = this.ChangeTracker.Entries().Where(e => e.State == System.Data.EntityState.Modified && e.Entity is myEntityName);
foreach (var item in modifiedItems)
{
//ToDo: Write UPDATE Sql Query here
}
return base.SaveChanges();
}
How can I write my update query?
Thanks!
This is not possible. You can only map custom stored procedures to be used instead of CUD operations (only with EDMX) generated by EF but still you will have single stored procedure call per each operation and entity instance.

Entity Framework, mapping Views to Tables

I have a basic view that returns the same columns as a table (give or take 1 field)
in my DAL code, i am returning a list of MyTableObject, however in some cases, i will call the view to return the same data, but from different sources.
List<MyTableObject> tableObjects = new List<MyTableObject>();
if (case1)
tableObjects = entities.MyTableObjects.Where(criteria).ToList();
else
tableObjects = entities.MyViewObjects.Where(criteria).ToList(); // <-- This will obviously break
return tableObjects;
is there a way to Map view entities to be returned as table entities? (other than having table and view implement the same interface and return that interface) i would like to keep the return type as MyTableObject.
I came across Auto Mapper, but not sure if it would be suitable for this scenario..
Looks like i found a cool solution to this..
Initially I tried to implement interface approach and run into some casting issues (using interfaces alongside my predicate builder), and also with interfaces having to create partial classes for each entity that implement the interface..
the answer.. POCOs.
Iused Poco Template for EF, and than simply edited xxxPocoGenerator.Context.tt to return MyTable object from MyViews collection (one line).
public ObjectSet<Trade> v_Trade {
get { return _v_Trade ?? (_v_Trade = CreateObjectSet<Trade>("Trades")); }
}
nice and easy..
You can write a stored procedure (or CommandText in the model, without creating DB Object) that will simply call "Select * from View". Then create Function Import for this procedure and set the return type to MyTableObject.

Order By property from dynamic linq

I am using dynamic linq to make a generic class for processing a generic JqGrid from MVC all works fine (searching, pagination etc) except for sorting on code properties. Sorting works fine when I am hitting the DB to sort the data, but as soon as it is a property I have made the sorting does not work eg
public partial class tblStockOrder
{
public string approved
{
get
{
return approved_id == null ? "" : "Approved";
}
}
}
I am running the following Dynamic Linq
items = items
.OrderBy(string.Format("{0} {1}", sidx, sord))
.Skip(pageIndex * pageSize)
.Take(pageSize);
Where sidx etc are strings passed in by jquery.
So basically what is the best solution for handling a case where some properties will be from the db while others will be code properties (not sure of the correct naming). I can handle all this in code using reflection but would obviously like the DB to handle as much of the searching / sorting as possible without pulling in thousands of records and sorting through them in code using reflection.
Computed class will of course not work, as you're trying to create record which is part in memory, part in database.
You can, however, compute the same on database by specifying the function in linq query, example:
items = items
.OrderBy(x=> x.approved_id != null )
.Skip(pageIndex * pageSize)
.Take(pageSize);

Resources