Group by a non Database field

2 posts, 0 answers
  1. Manuel
    Manuel avatar
    43 posts
    Member since:
    Jul 2010

    Posted 08 Dec 2015 Link to this post

    Dear all,

     in an entity i have two Properties.

    * ProductLine

    * ProductLineDisplay

    ProductLine is stored in the db, ProductLineDisplay returns a string only.

     Now I like to group by ProductLine (because if I group by ProductLineDisplay an exception is raised) and display the ProductLineDisplay property. How is this possible?

    var result = from o in Context.StatisticOrders
                            join ol in Context.StatisticOrderLines
                            on o.Id equals ol.StatisticOrderId
                            where o.Date >= start && o.Date <= end && o.AddressId == address
                            group ol by ol.ProductLine into gr
                            select new SalesChart
                            {
                                ProductLine = gr.Key.ToString(),
     
                                ProductLineDisplay = ?
                                Sales = gr.Sum(x => x.Price)
                            };

    Thank you,

    Manuel

  2. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 08 Dec 2015 Link to this post

    Hello Manuel,

    I think there is no way you can use the instance property ProductLineDisplay, because at the point where you need this, it can't be accessed as you are in a group level there, not in the groups elements.

    My best advice is to make a 
    public static string MakeDisplay<T>(T key) // replace T with your real key type
    {
        return string.Format("WhatEver that means {0}", key);
    }

    and call that in the final Select() clause.

    Regards,
    Thomas
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
Back to Top