How call a custom method in lambda expression . I Use Entity Framework 4 . Stored expression error - entity-framework

Is it possible to call a custom method in a lambda expression.?
//Address a : It's an Entity
public string AddressConstructor(Address a)
{
return a.City + "," + a.Province;
}
var Test = _db.MyTableTest.Select( t => new ViewModel
{
MyID = t.ID,
StringAddress = AddressConstructor(t.Addr)
};

You should be able to accomplish this using LINQKit to inline your expression into the expression tree.
http://www.albahari.com/nutshell/linqkit.aspx
This will cause the concatanation you're attempting to be run on the SQL Server, not in memory (as described in the other answer). SQL Server of course knows how to concatanate strings, but if your AddressConstructor did something else that SQL Server doesn't understand, then this approach would not work and you would indeed need to perform your custom method in memory using the approach described in the other answer.
Essentially LINQKit will flatten the tree so it actually gets executed as:
var Test = _db.MyTableTest.Select( t => new ViewModel
{
MyID = t.ID,
StringAddress = t.Addr.City + "," + t.Addr.Province
};
which EF should have no problem executing (and the concatenation should happen on the SQL Server).

You need to call AsEnumerable so that the projection is executed locally:
var Test = _db.MyTableTest.AsEnumerable()
.Select( t => new ViewModel
{
MyID = t.ID,
StringAddress = AddressConstructor(t.Addr)
};
Otherwise, the Queryable.Select method is used instead of Enumerable.Select, which causes Entity Framework to try to translate the lambda expression to a SQL query (which of course is not possible in that case)

Related

Reusable Functions in Linq To Entites

I have 2 reusable functions that return lists. If the code from these functions is written directly into the linq to entities query all is good. However, separating these out into functions causes an error as it cannot be translated to a stored expression. I'm sure there must be a way of doing this though. Any ideas how to solve this problems. Ideally I want the reusable parts to be used outside of linq to entity queries also.
var activityBands = DbContext.ActivityBand
.OrderBy(x => x.ActivityBandDescription)
.Where(x => x.Active && x.ClientAccountId == clientAccountId)
.Select(x => new ActivityBandDdl
{
Name = x.ActivityBandDescription,
ActivityBandId = x.ActivityBandId,
ApplyAwr = x.ApplyAwr,
AssignmentLineTimeTypeIds = TimeTypesForActivityBand(x.DailyRate) ,
AssignmentTypeIds = AssTypesForActivityBand(x.StagePayment)
}).ToList();
public static Func<bool, List<int>> TimeTypesForActivityBand =
(dailyRate) => (new int[] { 1, 2, 3, 4 }).Where(t =>
((t != 1 && t != 2) || !dailyRate) //No Timed or NTS for daily rates
).ToList();
public static Func<bool, List<int>> AssTypesForActivityBand =
(stagePayment) => (new int[] { 2,3,4,5,6,7,8,9,10 }).Where(t =>
( t!=2 || !stagePayment) //Only stage pay ass have stage pay activity bands
).ToList();
TL;DR;
suggested solution for your problem:
get LinqKit ... have a look at it's Expand() function (in the docs: combining expressions)
https://github.com/scottksmith95/LINQKit#combining-expressions
the details:
the problem boils down to: what is the difference between the queries in both cases...
A LINQ query works with an expression tree ... in other words: just because the code you typed directly into the query and the code you typed into the static Func<...> looks the same, in fact, is the same, the resulting expression trees in both cases are not the same
what is an expression tree?
imagine a simpler query like ... someIQueryable.Where(x => x.a==1 && x.b=="foo")
the lamda that is passed to Where(...) can be seen as a straigt forward c# lambda expression that can be used as a Func
and it can also be seen as a Expression>
the later is a tree of objects that form the expression, in other words a description about the way, in that the passed in parameter can be evaluated to a bool without actually having the executable code, but just the description about what to do ... take the member a from the parameter, equality-compare it to the constant 1 ... take the boolean AND of the result with the result of: take the member b from the parameter, equality-compare it to the constant "foo" ... return the result of the boolean AND
why all of this?
it's the way LINQ works ... LINQ to entiteis takes the expression tree, looks at all the operations, finds the corresponding SQL, and builds an SQL statement which is executed in the end ...
when you have your extracted Func<...> there is a little problem ... at some point in the resulting expression tree there is something like ... take the parameter x and CALL the static Func ... the expression tree does no longer contain a description of whats happening inside the Func, but just a call to that ... as long as you want to compile that to a .net runtime executable function, it's all fun and games ... but when you try to parse it into SQL, LINQ to entiteis does not know a corresponding SQL for "call some c# function" ... therefore it tells you that this part of the expression tree can not be converted into a store expression

EF6 Call stored procs using SqlQuery/ExecuteSqlCommand without regard for parameter order?

I am using EF 6 SqlQuery/ExecuteSqlCommand to call stored procedures like so:
var parameters = new List<SqlParameter>()
{
new SqlParameter("#ID", model.ID),
new SqlParameter("#Email", model.Email),
new SqlParameter("#Phone", model.Phone)
};
using (var context = new MyContext())
{
context.Database.ExecuteSqlCommand("sp_MySproc_U #ID, #Email, #Phone", parameters.ToArray());
}
This works great, until I realized that my Email values were being saved to my Phone field and vice versa. Why? Because the order of the parameters in my stored procedure is #ID, #Phone, #Email, which obviously doesn't match the order of the parameters in my code. Is there a simple way in the SqlQuery/ExecuteSqlCommand syntax to specifically name the parameters and match them with values. My goal is to call my stored procs without being concerned with the order of the parameters in code matching the order in the stored procedure.
Someone in this post suggested the following,
db.Database.SqlQuery<resultClass>("mySpName PageNumber=#PageNumber,Code=#Code,PageSize=#PageSize", parameters.ToArray()).ToList();
but this returns and "Invalid character '='" error. Thanks for your help.
According to the SQL Server stored procedure call syntax, the named parameters must be preceeded by at sign #, i.e
#parameter=value
or
#parameter=#variable
So you could use
context.Database.ExecuteSqlCommand(
"sp_MySproc_U #ID=#ID, #Email=#Email, #Phone\#Phone",
parameters.ToArray());
or more generically
context.Database.ExecuteSqlCommand(
"sp_MySproc_U " + string.Join(", ", parameters.Select(p => p.Name + "=" + p.Name)),
parameters.ToArray());

LINQ to Entities does not recognize the method - Include + Where

I have this working Code
var h = db.MyTable.Include("Children").Include("Parent").ToList();
but when I add where condition
var h = db.MyTable.Include("Children").Include("Parent").Where(x => x.Country==Session["country"].ToString()).ToList();
it throws me an error
LINQ to Entities does not recognize the method 'System.Object get_item (System.String)' method , and this method cannot be translated into a store expression.
How can i rewrite it ? Im beginner :-)
Thank you
This is caused by your Where-Expression. You should only use variables in there or call methods that can be translated to SQL.
First, save your Session value in a variable:
var country = Session["country"].ToString();
Then use country in your Where-Expression.
MSDN provides a list of methods you can use inside LINQ to SQL expressions and how they are mapped to SQL functions.
try this:
var t = Session["country"].ToString();
var h = db.MyTable.Include("Children").Include("Parent").Where(x => x.Country==t).ToList();
only activities can be parsed into expression-tree that is supported by your provider

How to refactor parts of an EF select

Let's say I have the following EF code:
context.Employees.Select(e => e
{
FullName = e.FirstName + " " + e.LastName,
StartDate = e.StartDate,
... // Grab other data
};
Now maybe I notice that I construct the full name in multiple places, but would like in centralized. Is it possible to refactor this?
If I make it a method or a Func, I get EF errors, because it can't translate it into SQL.
NOTE: This is a simple example, assume it can get much more complicated with "Select"s, "Where"s, whatever in the assignment, so adding a ToList and then running additional code would be suboptimal and does not fit the definition of refactoring since I would have to change functionality and not just make it more maintainable.
One solution is to use the AsExpandable method from LinqKit:
Expression<Func<Employee,string>> fullName = e => e.FirstName + " " + e.LastName;
context.Employees.AsExpandable().Select(e => e
{
FullName = fullName.Compile().Invoke(e),
StartDate = e.StartDate,
... // Grab other data
};
From the linked article:
Compile is an inbuilt method in the Expression class. It converts the
Expression into a plain Func which
satisfies the compiler. Of course, if this method actually ran, we'd
end up with compiled IL code instead of an expression tree, and LINQ
to SQL or Entity Framework would throw an exception. But here's the
clever part: Compile never actually runs; nor does LINQ to SQL or
Entity Framework ever get to see it. The call to Compile gets stripped
out entirely by a special wrapper that was created by calling
AsExpandable, and substituted for a correct expression tree.
Alternatively, you could look into creating an Model Defined Function with Entity Framework. There's also the Microsoft.Linq.Translations library if you want to define a FullName property on the Employee class itself.
I think the better centralized way to do it in the entity class itself. You can add ReadOnly property to your entity class which should be NotMapped to the database to return required formatted data.
Public class Employee
{
//...
public string fullName{get { return FirstName + " " + LastName;}}
}

Generated SQL with PredicateBuilder, LINQPad and operator ANY

I previously asked a question about chaining conditions in Linq To Entities.
Now I use LinqKit and everything works fine.
I want to see the generated SQL and after reading this answer, I use LinqPad.
This is my statement:
var predProduct = PredicateBuilder.True<Product>();
var predColorLanguage = PredicateBuilder.True<ColorLanguage>();
predProduct = predProduct.And(p => p.IsComplete);
predColorLanguage = predColorLanguage.And(c => c.IdColorEntity.Products.AsQueryable().Any(expr));
ColorLanguages.Where(predColorLanguage).Dump();
The code works in VS2008, compile and produce the correct result set, but in LinqPad, I've the following error:
NotSupportedException: The overload query operator 'Any' used is not Supported.
How can I see the generated SQL if LINQPad fails?
EDIT
If I write
var predColorLanguage = PredicateBuilder.True<ColorLanguage>();
predColorLanguage = predColorLanguage.And(c => c.IdColorEntity.Products.Any((p => p.IsComplete));
ColorLanguages.Where(predColorLanguage).Dump();
works... WTF?
As you're using LINQKit, you can make this work by calling Compile() on the expression that feeds the EntitySet, and then calling AsExpandable() on the main query:
var predProduct = PredicateBuilder.True<Product>();
var predColorLanguage = PredicateBuilder.True<ColorLanguage>();
predProduct = predProduct.And(p => p.IsComplete);
predColorLanguage = predColorLanguage.And (
c => c.IdColorEntity.Products.Any(predProduct.Compile()));
ColorLanguages.AsExpandable().Where(predColorLanguage).Dump();
As explained in the LINQKit article, the Compile method never actually runs: AsExpandable strips it out and modifies the expression tree so that it works with LINQ to SQL.