locked
another option instead using AsEnumerable() in linq EF, when using custom method in linq RRS feed

  • Question

  • User2131089582 posted

    another option instead using AsEnumerable() in linq EF, when using custom method in linq

    I have below code
    class Program
    {
    static void Main(string[] args)
    {
    string keyword = "m";
    using (TestingEntities1 db = new TestingEntities1())
    {
    var models = db.Teachers.AsEnumerable().Where(a => RemoveA(a.Name).Contains(keyword));
    foreach (var item in models)
    {
    Console.WriteLine(item.Name);
    }
    }
    }
    static string RemoveA(string input)
    {
    return input.Replace('a', ' ');
    }
    }

    as you can see in my code i must use AsEnumerable() to use custom function works because if i dont use i will get something error saus
    "LINQ to Entities does not recognize the method ' ' method, and this method cannot be translated into a store expression."

    and then i ralized that asenumerable make slow becaseu i found that here https://stackoverflow.com/questions/21682125/how-to-improve-asenumerable-performance-in-ef says "you're doing is that you're downloading the whole BilBillMasters and BilBillDetails tables and then doing some processing on those in your application, rather than on the SQL server. This is bound to be slow."
    please see first anwer for more detail, So is there any way to make my code faster, my case i have more than one million data in my database

    Saturday, October 19, 2019 7:36 AM

All replies

  • User1120430333 posted

     RemoveA() is some function you wrote. EF can't convert the code in the function you wrote into T-SQL formulated by a Linq-2-Entities query, and that's what the error message means. It's not advisable that you use some function you wrote as part of a Linq-2-Entites  query involving EF.

    You can do what your doing with Linq-2-Objects, since that doesn't result in T-SQL being generated  by the EF engine using Linq-2-Entities.

    Linq-2-Entities  and EF are not a panacea solution for everything, and maybe you should consider using a stored procedure.

    Saturday, October 19, 2019 9:57 AM
  • User475983607 posted

    This is a design bug.  It does not matter if the query is written in LINQ or a stored procedure (T-SQL).  The logic must read every single record to remove the "a" then filter.  I'm sure there is a reasonable solution once you explain the filter requirement.  

    Saturday, October 19, 2019 11:11 AM
  • User2131089582 posted

    how do i do it ?

    Saturday, October 19, 2019 12:48 PM
  • User475983607 posted

    hocamahdi99

    how do i do it ?

    It is up to you.  Either come with us a better design or explain to the community why you need to replace the letter "a" with a space.  Once we understand what the problem, replace "a" with a space solves, we can provide a suggestion.  

    My initial recommendation is fixing the code that inserts the unwanted character(s).

    Saturday, October 19, 2019 1:04 PM
  • User2131089582 posted

    actually this is what i want https://forums.asp.net/t/2160200.aspx?+cannot+replace+diacritics+%D9%91+sign+in+arabic

    but no body answer it, just wrong anserw

    Saturday, October 19, 2019 1:26 PM
  • User475983607 posted

    actually this is what i want https://forums.asp.net/t/2160200.aspx?+cannot+replace+diacritics+%D9%91+sign+in+arabic

    but no body answer it, just wrong anserw

    The diacritics accent is not a separate character and cannot be replaced.  For the third time, can you explain the problem you are trying to solve?

    Saturday, October 19, 2019 1:44 PM