add orderby to linq not working

Hello all,

Using linq, how can I add an ORDERBY feature to my existing query?

I want to orderby (double)item.Element("Settle_x0020_PL")
But I can't seem to get it working.

thanks in advance!

                var breakdownSourceData =
                    from item in libraryXdoc.Descendants("Table")
                    group item by new
                    {
                        mName = (string)item.Element("Market_x0020_Name")
                    } into g
                    
                    select new
                    {
                        MarketName = (string)g.Key.mName,
                        Delta = (decimal?)g.Where(item => (string)item.Element("Trade_x0020_Date") == priorDate).Elements("Delta_x0020_Settle").FirstOrDefault(),
                        PrevPos = (long)g.Where(item => (string)item.Element("Trade_x0020_Date") == priorDate).Sum(item => (long)item.Element("Qty")),
                        TopTrd = (long)g.Where(item => (string)item.Element("Trade_x0020_Date") == todayDate).Sum(item => (long)item.Element("Qty")),
                        NetPos = (long)g.Sum(item => (long)item.Element("Qty")),
                        PrevPL = (double)g.Where(item => (string)item.Element("Trade_x0020_Date") == priorDate).Sum(item => (double)item.Element("Settle_x0020_PL")),
                        TopPL = (double)g.Where(item => (string)item.Element("Trade_x0020_Date") == todayDate).Sum(item => (double)item.Element("Settle_x0020_PL")),
                        NetPL = (double)g.Sum(item => (double)item.Element("Settle_x0020_PL"))
                    };

Open in new window

solarissfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi solarissf;

Not sure how you want this ordered. You say you want it ordered by (double)item.Element("Settle_x0020_PL"), but that is not a member of the result set. You do have a calculated value of (double)item.Element("Settle_x0020_PL") which is call NetPL. If that is what you want it sorted by then the following query should do what you want.

var breakdownSourceData =
    (from item in libraryXdoc.Descendants("Table")
     group item by new
     {
         mName = (string)item.Element("Market_x0020_Name")
     } into g
     
     select new
     {
         MarketName = (string)g.Key.mName,
         Delta = (decimal?)g.Where(item => (string)item.Element("Trade_x0020_Date") == priorDate).Elements("Delta_x0020_Settle").FirstOrDefault(),
         PrevPos = (long)g.Where(item => (string)item.Element("Trade_x0020_Date") == priorDate).Sum(item => (long)item.Element("Qty")),
         TopTrd = (long)g.Where(item => (string)item.Element("Trade_x0020_Date") == todayDate).Sum(item => (long)item.Element("Qty")),
         NetPos = (long)g.Sum(item => (long)item.Element("Qty")),
         PrevPL = (double)g.Where(item => (string)item.Element("Trade_x0020_Date") == priorDate).Sum(item => (double)item.Element("Settle_x0020_PL")),
         TopPL = (double)g.Where(item => (string)item.Element("Trade_x0020_Date") == todayDate).Sum(item => (double)item.Element("Settle_x0020_PL")),
         NetPL = (double)g.Sum(item => (double)item.Element("Settle_x0020_PL"))
     }).OrderBy( n => n.NetPL);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
solarissfAuthor Commented:
yes you were correct, that's what I was looking for and it worked!
thanks!!!
0
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.  ;=)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.