7888
7888

Reputation: 72

Linq ordering output

I am trying to sort a listbox by the customerID and then by Total (discount*unitPrice*quantity) and cannot manage to organize the code in a way that will sort it in that way. Any help would be greatly appreciated.

HERE is a link showing an image on how the results should be returned as.

var load1 = System.IO.File.ReadAllLines(@"c:\temp\AS3Products.csv")
            .Select(x => new
            {
                CID = x.Split(',')[0],
                discount = x.Split(',')[2].Trim(),
                productId = x.Split(',')[0].Trim()
            });

var load2 = System.IO.File.ReadAllLines(@"c:\temp\AS3Transactions.csv")
            .Select(x => new
            {
                productId = x.Split(',')[3],
                unitPrice = x.Split(',')[4],
                quantity = x.Split(',')[5]
            });

var querypractice = from x in load1
                    join y in load2 on x.productId equals y.productId 
                    where x.CID == "110"
                    orderby x.discount, y.quantity
                    select new { x.CID, x.discount, x.productId, y.quantity, y.unitPrice };

foreach (var x in querypractice)
{
    double total = double.Parse(x.quantity) * double.Parse(x.unitPrice) * double.Parse(x.discount);
    listBox1.Items.Add(x.CID+ "   " +x.discount+"   "+x.quantity+ "   " + total);
}

Upvotes: 1

Views: 63

Answers (2)

Grant Winney
Grant Winney

Reputation: 66439

If you're positive that these files have numbers in the expected places all the time, you could parse them as you read them from the files. Otherwise, you'll want to do some validation first or you'll get exceptions.

(I changed double.Parse to decimal.Parse - it's more accurate for manipulating dollar amounts.)

 var load1 = System.IO.File.ReadAllLines(@"c:\temp\AS3Products.csv")
 .Select(x => new
 {
     CID = int.Parse(x.Split(',')[0]),
     discount = decimal.Parse(x.Split(',')[2].Trim()),
     productId = int.Parse(x.Split(',')[0].Trim())
 });

 var load2 = System.IO.File.ReadAllLines(@"c:\temp\AS3Transactions.csv")
             .Select(x => new
             {
                 productId = int.Parse(x.Split(',')[3]),
                 unitPrice = decimal.Parse(x.Split(',')[4]),
                 quantity = int.Parse(x.Split(',')[5])
             });

Then you can create your list like this. (I removed the specific id you had in your query.)

 var orderedList = (from x in load1
                    join y in load2 on x.productId equals y.productId
                    let total = (x.discount * y.unitPrice * y.quantity)
                    orderby x.CID descending, total descending
                    select new
                    {
                        x.CID,
                        x.discount,
                        x.productId,
                        y.quantity,
                        y.unitPrice
                    });

Upvotes: 1

Jon Barker
Jon Barker

Reputation: 1828

Disclaimer: I don't have VS on this machine, so this isn't validated, but I think you can do it using the LET statement to set up the calculated value, then order based on it.

var querypractice = from x in load1
                join y in load2 on x.productId equals y.productId 
                let total = x.discount*x.unitPrice*x.quantity
                where x.CID == "110"
                orderby x.CID, total
                select new { x.CID, total };

http://www.codeproject.com/Articles/231164/Into-and-let-in-LINQ-Let-vs-Into

Upvotes: 1

Related Questions