1

In Entity framework I have objectsets like

public partial class Building
{       
    public int BuildingID { get; set; }
    public string BuildingName { get; set; }
}
public partial class Town
{       
    public int TownID { get; set; }
    public string TownName { get; set; }
}

I want to create a generic query like

T.OrderBy(o=>o.Id).Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();

T is generic class can be Building or Town but problem is BuildingId and TownId has different name.I don't want to change their name as Id and create interface IIdentity.

Horizon
  • 75
  • 3
  • 11
  • If you cannot create an Interface than your only other option is a base class that Building and Town inherit from. – mxmissile Aug 13 '14 at 15:03

2 Answers2

0

Maybe you could try something like this:

var query = (typeof(T) == typeof(Building) ?
                context.Buildings.Select(b => new { Id = b.BuildingId, Name = b.BuildingName }) :
                context.Towns.Select(t => new { Id = t.TownId, Name = b.TownName }))
            .OrderBy(o => o.Id)...

Not tested but that's worth a test...

Pragmateek
  • 12,648
  • 9
  • 66
  • 105
0

You can create generic method which find a field decorated with KeyAttribute, and then performs sorting by found key field. I have tested your model, works perfectly. Look at code snippet.

DbContext:

using System.Collections.Generic;
using System.Data.Entity;

namespace ConsoleApplication28.Entities
{
    public class AppDbContext : DbContext
    {
        public AppDbContext()
        {
            Database.Connection.ConnectionString = @"Data Source=NOTEBOOK-PC;Initial Catalog=StackOverflowTest;Integrated Security=True";
            Database.SetInitializer(new AppDbInitializer());
        }

        public DbSet<Town> Towns { get; set; }
        public DbSet<Building> Buildings { get; set; }
    }

    public class AppDbInitializer : DropCreateDatabaseIfModelChanges<AppDbContext>
    {
        protected override void Seed(AppDbContext context)
        {
            context.Buildings.AddRange(new List<Building>
                                       {
                                           new Building {BuildingName = "Building1"},
                                           new Building {BuildingName = "Building2"},
                                       });

            context.Towns.AddRange(new List<Town>
                                       {
                                           new Town {TownName = "Town1"},
                                           new Town {TownName = "Town2"},
                                       });
            context.SaveChanges();
            base.Seed(context);
        }
    }
}

Building

using System.ComponentModel.DataAnnotations;

namespace ConsoleApplication28.Entities
{
    public class Building
    {
        [Key]
        public int BuildingID { get; set; }
        public string BuildingName { get; set; }
    }
}

Town

using System.ComponentModel.DataAnnotations;

namespace ConsoleApplication28.Entities
{
    public class Town
    {
        [Key]
        public int TownID { get; set; }
        public string TownName { get; set; }
    }
}

Program

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ConsoleApplication28.Entities;
using System.ComponentModel.DataAnnotations;

namespace ConsoleApplication28
{
    class Program
    {
        static void Main(string[] args)
        {
            const int maxDispItem = 10;
            const int page = 1;
            var db = new AppDbContext();
            var towns = db.Towns.OrderByKey().Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
            var buildings = db.Buildings.OrderByKey().Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
        }
    }

    public static class Extensions
    {
        /// <summary>
        /// Sorts the elements of a sequence in ascending order according to a key specified using KeyAttribute
        /// </summary>
        public static IOrderedQueryable<T> OrderByKey<T>(this IQueryable<T> source, bool isAsc = true)
        {
            var type = typeof(T);
            var keyProperty = type.GetProperties().Single(x => x.GetCustomAttributes(typeof(KeyAttribute)).Any());
            return source.OrderBy(keyProperty.Name, isAsc);
        }

        #region COPIED FROM THERE http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet

        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property, bool isAsc)
        {
            return isAsc ? source.OrderBy(property) : source.OrderByDescending(property);
        }
        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "OrderBy");
        }
        public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "OrderByDescending");
        }
        public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "ThenBy");
        }
        public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
        {
            return ApplyOrder<T>(source, property, "ThenByDescending");
        }
        static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
        {
            string[] props = property.Split('.');
            Type type = typeof(T);
            ParameterExpression arg = Expression.Parameter(type, "x");
            Expression expr = arg;
            foreach (string prop in props)
            {
                PropertyInfo pi = type.GetProperty(prop);
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }
            Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
            LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

            object result = typeof(Queryable).GetMethods().Single(
                    method => method.Name == methodName
                            && method.IsGenericMethodDefinition
                            && method.GetGenericArguments().Length == 2
                            && method.GetParameters().Length == 2)
                    .MakeGenericMethod(typeof(T), type)
                    .Invoke(null, new object[] { source, lambda });
            return (IOrderedQueryable<T>)result;
        }

        #endregion
    }
}
testCoder
  • 6,587
  • 13
  • 49
  • 72