This is a migrated thread and some comments may be shown as answers.

Group by a non Database field

1 Answer 25 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Manuel
Top achievements
Rank 1
Manuel asked on 08 Dec 2015, 03:16 PM

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

1 Answer, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 08 Dec 2015, 03:58 PM
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.
Tags
LINQ (LINQ specific questions)
Asked by
Manuel
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Share this question
or