Use of “Single” in Dynamic Linq

后端 未结 2 1803
栀梦
栀梦 2021-01-14 09:50

I am trying to convert a Linq query that I have working in Linq to be able to work in dynamic linq (using System.Linq.Dynamic) because I want a user to be able to form their

2条回答
  •  粉色の甜心
    2021-01-14 10:41

    Sorry to digg up a very old thread, but I thought I could add somevaluable information!

    I had to do this for First()/FirstOrDefault() with Linq to Entities instead of your Linq to SQL and I can confirm @Guillaume86 's solution sure works!

    Here's how I modified MicrosoftDynamic.sql: I added this inside the static DynamicQueryable class:

        public static object FirstOrDefault(this IQueryable source)
            {
                if (source == null) throw new ArgumentNullException("source");
                return source.Provider.Execute(
                    Expression.Call(
                        typeof(Queryable), "FirstOrDefault ",
                        new Type[] { source.ElementType },
                        source.Expression));
            }
    

    I also modified interface IEnumerableSignatures as

                void FirstOrDefault();
    

    (I used FirstOrDefault because First() isn't supported when it's not the last call in linq to entities)

    You can repeat this for any supported function :)

提交回复
热议问题