Frage

I am having a strange issue, i am writing out a small method to use the System.Linq.Dynamic classes to query a list of objects though i do not think my issue lies with the Dynamic namespace. For some reason GroupBy and OrderByDescending cannot bed found. I already have a reference to System.Linq which is what i initially thought would have caused this... Weirdly it is finding OrderBy and other Linq methods.. My Code is below.

using System;
using System.Collections.Generic;
using System.Linq.Dynamic;
using System.Linq;
using System.Web;

namespace MvcTemplate.jrSite.Core
{
    public static class DynamicQuery
    {
        public static IQueryable Query(IQueryable items, string where, string select, string groupby, string orderby, string orderbydesc, int take)
        {
            var ret = items;

            if (where != null && where.Length != 0)
            {
                ret = ret.Where(where);
            }

            if (select != null && select.Length != 0)
            {
                ret = ret.Select(select);
            }

            if (groupby != null && groupby.Length != 0)
            {
                ret = ret.GroupBy(groupby);
            }

            if (orderby != null && orderby.Length != 0)
            {
                ret = ret.OrderBy(orderby);
            }

            if (orderbydesc != null && orderbydesc.Length != 0)
            {
                ret = ret.OrderByDescending(orderbydesc);
            }

            return ret;
        }
    }
}
War es hilfreich?

Lösung

I assume that you are using System.Linq.Dynamic project. Your methods are not found because they do not exist.

1) GroupBy form System.Linq.Dynamic:

public static IQueryable GroupBy(this IQueryable source, 
    string keySelector, string elementSelector, params object[] values) {

this method requires three parameters and your code provides two - you have to provide elementSelector.

2) The method OrderByDescending is not even declared in the source of that library. And System.Linq will not help you either as you are using non generic IQueryable. All extension methods are provided only for strongly typed queries.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top