none
Entity framework Expressions and Contains RRS feed

  • Question

  • Hi,

    If I have a List<int> (myList), this query works:

    from x in db.mytable
    where myList.Contains(x.Id)
    select x

    However, if I build in this way, it doesn't work:

    public Expression<Func<T,bool>> myReusePredicate(List<int> myList) where T: objWithId =>
          (R) => myList.Contains(R.Id)

    ====

    db.mytable.where(myReusePredicate(myList)).ToList()   <=  This fails, complaining about the List<int> type.


    I investigated a bit about ExpressionVisitors, but have not found the solution yet.

    Thank you for any help!

    Dennes



    • Edited by Dennes Tuesday, September 10, 2019 10:43 PM
    Tuesday, September 10, 2019 10:42 PM

All replies

  • Does it work if it returns ‘<Func<T, bool>’ instead of ‘Expression<Func<T, bool>>’?
    Wednesday, September 11, 2019 4:55 AM
  • Hi Dennes,

    Thank you for posting here.

    For your question, you want to use Expression to select related data from db.mytable.

    I make a simple test on my side using Expression.

        class Program
        {
            public static List<int> myList = new List<int>();
            public static Expression<Func<Student, bool>> myReusePredicate = i => myList.Contains(i.Id);
            static void Main(string[] args)
            {
                myList.Add(220);
                myList.Add(221);
                List<Student> list = new List<Student>();
                list.Add(new Student { Id = 220 });
                list.Add(new Student { Id = 221 });
                list.Add(new Student { Id = 210 });
                Func<Student, bool> func = myReusePredicate.Compile();
                List<Student> li = new List<Student>();
                foreach (var item in list)
                {
                    if (func(item) == true)
                    {
                        li.Add(item);
                    }
                }
            }
        }
    
            class Student
            {
                public int Id { get; set; }
            }

    Hope it can help you.

    Besides, If I have any misunderstanding, please provide more informations.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 11, 2019 7:50 AM
    Moderator
  • Hi,

    Thank you!

    However, your datasource is a list, you are only using linq (you had to compile the expression). My problem are on limitations of linq to entities, using a database.

    Kind Regards,

    Dennes


    Dennes [http://bufaloinfo.cloudapp.net] Inscreva-se em meu treinamento on-line de T-SQL - [http://bufaloinfo.cloudapp.net/Cursos/linguagemsql.aspx]

    Wednesday, September 11, 2019 8:03 AM
  • Hi,

    I need to apologize for the impossible question, I end up finding the answer.

    When I wrote the question, I simplified the code to make the understanding easier. 

    However, the problem was exactly because I was using complex objects on my queries. When I simplified and converted the objects to simple List<int> and used the contains method, everything worked find.

    Thank you!


    Wednesday, September 11, 2019 2:52 PM
  • Hi Dennes,

    It seems that your problem has been solved. If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Thank you for your support.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 12, 2019 9:06 AM
    Moderator