none
Why we need to use <T> or <TOrderKey> when using predicate for OrderBy RRS feed

  • Question

  • i found a piece of code where search and orderby performed by generic from this url https://stackoverflow.com/questions/23778903/repository-method-accepting-predicate-and-orderby

    now see the code

    public async Task<IList<Person>> SearchForPeople(Expression<Func<Person, bool>> predicate, Expression<Func<Person, object>> orderBy)
    {
        return await this.context.People.Where(predicate).OrderBy(orderBy).ToListAsync();
    }
    var people = await repository.SearchForPeople(x => x.Born.Day == now.Day && x.Born.Month == now.Month, x => x.OrderingKey);

    the above code is working but the below code is not working when user try to order by different column. see it

    var people = await repository.SearchForPeople(x => x.Born.Day == now.Day && x.Born.Month == now.Month, x => x.Born);

    the above code throwing error just because date column is used in order by clause ?

    solution 1
    --------------
    one guys said use <T> like

    public async Task<IList<Person>> SearchForPeople<T>(Expression<Func<Person, bool>> predicate, Expression<Func<Person, T>> orderBy)
    {
        return await this.context.People.Where(predicate).OrderBy(orderBy).ToListAsync();
    }
    
    var people = await repository.SearchForPeople(x => x.Born.Day == now.Day && x.Born.Month == now.Month, x => x.Born);

    solution 2
    --------------
    another guys said use <TOrderKey> like

    public async Task<IList<Person>> SearchForPeople<TOrderKey>(Expression<Func<Person, bool>> predicate, Expression<Func<Person, TOrderKey>> orderBy)
    {
        return await this.context.People.Where(predicate).OrderBy(orderBy).ToListAsync();
    }

    what is <TOrderKey> ?

    so my question is why we need to use generic <T> or <TOrderKey> to solve this problem. without generic can't we do the sorting?

    please guide me details


    • Edited by Sudip_inn Tuesday, March 13, 2018 3:13 PM
    Tuesday, March 13, 2018 3:11 PM

Answers

  • Hi Sudip_inn,

    >>what is <TOrderKey> ?

    It is generic parameter. same as T, the two solution are same, just like variable name, you could define a variable with name a, you could also define the variable with name b. For more information about generic, please refer to:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    >>so my question is why we need to use generic <T> or <TOrderKey> to solve this problem. without generic can't we do the sorting?

    I would suggest that you use generic parameter when you want to use dynamic order instead of you order by fields have the same type, such as string. then you could use string replace generic.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, March 14, 2018 2:40 PM
    Wednesday, March 14, 2018 7:25 AM
    Moderator

All replies

  • Hi Sudip_inn,

    >>what is <TOrderKey> ?

    It is generic parameter. same as T, the two solution are same, just like variable name, you could define a variable with name a, you could also define the variable with name b. For more information about generic, please refer to:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    >>so my question is why we need to use generic <T> or <TOrderKey> to solve this problem. without generic can't we do the sorting?

    I would suggest that you use generic parameter when you want to use dynamic order instead of you order by fields have the same type, such as string. then you could use string replace generic.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, March 14, 2018 2:40 PM
    Wednesday, March 14, 2018 7:25 AM
    Moderator
  • you said generic parameter when you want to use dynamic order instead of you order by fields have the same type

    public async Task<IList<Person>> SearchForPeople(Expression<Func<Person, bool>> predicate, Expression<Func<Person, object>> orderBy)
    {
        return await this.context.People.Where(predicate).OrderBy(orderBy).ToListAsync();
    }
    
    var people = await repository.SearchForPeople(x => x.Born.Day == now.Day && x.Born.Month == now.Month, x => x.ID);

    see the above code where we not use generic parameter but it works when order by column data type is integer but then moment order by column data type change may be string or datetime then code is not working. can you please tell me the reason.

    thanks

    Thursday, March 15, 2018 8:30 AM