Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Type alias

Back to Forum: Tech Off
  • User profile image
    PerfectPhase

    Is there a way to define somthing in my assembly along the lines of

     

    MyQuery<T> = Expression<Func<IQueryable<T>, IQueryable<T>>>

     

    I'm feed up of typing this over and over again!

     

    Cheers,

     

    Stephen.

  • User profile image
    GamlerHart

    I don't know any way. You can declare alias with a using-directive.

     

    using MyQuery = System.Linq.Expressions.Expression<System.Func<System.Linq.IQueryable<int>, System.Linq.IQueryable<int>>>;

     

    However it doesn't work to declare a 'generic' alias Sad :

    // Does NOT work =( 
      
    using MyQuery<T> = System.Linq.Expressions.Expression<System.Func<System.Linq.IQueryable<T>, System.Linq.IQueryable<T>>>; 

     

     

     

  • User profile image
    figuerres

    static global method that acts as a Factory?

     

    Foo<T>();

    or Foo<T>( TypeThing T);

     

    that retun an instance of the target Type ?

     

  • User profile image
    PerfectPhase

    figuerres said:

    static global method that acts as a Factory?

     

    Foo<T>();

    or Foo<T>( TypeThing T);

     

    that retun an instance of the target Type ?

     

    About the best option I settled on was something along the lines of as static method on a class

     
    public static Expression<Func<IQueryable<T>, IQueryable<T>>> CreateQuery<T>(Expression<Func<IQueryable<T>, IQueryable<T>>> spec)

    {

       return spec;

    }

     

    And then in the app code

      

    var spec = DataRepository.CreateQuery<User>((x) => from u in x where u.FirstName == firstname select u) ;

     

    Seems the best compromise for readability in the app code.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.