Ali Youhanaei
Ali Youhanaei

Reputation: 384

can i use custom lambda method in entity framework?

i have some methods like:

public static string ToOtherFormat (this string inp)
{
  // some code to change inp
  return inp;
}

and in my select i want to have code like this:

var DetailMembers = db.TB_Members
                      .Where(x=> x.FName == obj.ToOtherFormat())
                      .Select( x=> new { name = (x.FName.ToOtherFormat() + " " + x.LName) , x.ActCode });

i try and just have error. is it possible?
thanks!
i receive this error in simple convert to integer

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.

with this code

 .Where(x => x.MemberID == Convert.ToInt32(Hmemid.Hash_two_Decrypt())

Upvotes: 0

Views: 300

Answers (2)

Ali Youhanaei
Ali Youhanaei

Reputation: 384

i found it on use .AsEnumerable() method like:

var DetailMembers = db.TB_Members.AsEnumerable()
                      .Where(x=> x.FName == obj.ToOtherFormat())
                      .Select( x=> new { name = (x.FName.ToOtherFormat() + " " + x.LName) , x.ActCode });

Upvotes: 0

Habib
Habib

Reputation: 223237

Looks like you are querying against the database. Your current query will get translated into SQL query and since SQL doesn't recognize your function that is why you get error.

You may get the data from the tables using a query without that function and then later do the formatting on the result set.

Upvotes: 1

Related Questions