Tech Off Thread

4 posts

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.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.