Order By in Linq

Hi everyone

I have the following drawback, I am obtaining the list of brands that belong to a certain company through a linq, but I must organize them according to the order in which each brand has been sold (descending)

This is the linq and the column that contains the sales values of each brand is called “Ventas Netas Act”

(From z In dtFinal.AsEnumerable Where z(“Sociedad”).ToString.Trim.Equals(“Ind Colombiana Cafe S.A.S”) Select (z(“LINEA NUEVA”))).Distinct.ToList

Hi @Juan_Esteban_Valencia

Use like this

(From z In dtFinal.AsEnumerable Where z(“Sociedad”).ToString.Trim.Equals(“Ind Colombiana Cafe S.A.S”) Select (z(“LINEA NUEVA”))).orderby descending.Distinct.ToList

Thanks
Ashwin.S

@Juan_Esteban_Valencia

Please try this

(From z In dtFinal
Where z("Sociedad").ToString.Trim.Equals("Ind Colombiana Cafe S.A.S")
Order By z("Ventas Netas Act") Descending
Select (z("LINEA NUEVA"))).Distinct.ToList()

I think this is the solution, but I am getting the following error

Assign: Object must be of type Double.

@Juan_Esteban_Valencia

Need to convert the column into Double data type

(From z In dtFinal
Where z("Sociedad").ToString.Trim.Equals("Ind Colombiana Cafe S.A.S")
Order By CDbl(z("Ventas Netas Act").ToString) Descending
Select (z("LINEA NUEVA"))).Distinct.ToList()

Excellent, it no longer throws an error, but I do not understand why he is not organizing it in a descending way, I ask you, he will be doing the sum of all the values in “Venta Neta ACT” of each “Nueva Linea”

This is the table I have

Sociedad LINEA NUEVA Ventas Netas Act
Ind Colombiana Cafe S.A.S Café Molido 3.574.326.788
Ind Colombiana Cafe S.A.S Expresso 6.749.913.425
Ind Colombiana Cafe S.A.S Via 5.897.977.821
Ind Colombiana Cafe S.A.S Granulado 30.996.504
Ind Colombiana Cafe S.A.S Expresso 769.013.840
Ind Colombiana Cafe S.A.S Café Molido 1.791.115.710
Ind Colombiana Cafe S.A.S Café Molido 2.840.688.102
Ind Colombiana Cafe S.A.S Café Molido 45.908.105
Ind Colombiana Cafe S.A.S Café Molido 6.218.319.019
Ind Colombiana Cafe S.A.S Café Molido 2.721.157.339
Ind Colombiana Cafe S.A.S Café Molido 5.144.180.207
Ind Colombiana Cafe S.A.S Art Café 25.964.740
Cameron’s Coffee Art Café 377.627.287
Cameron’s Coffee Café Molido 1.394.387.184
Cameron’s Coffee Café Molido 2.241.497.043

It would be to obtain the lines that belong to Ind Colombiana Cafe S.A.S and organize them in descending order with respect to the sum of their Ventas Netas Act

@Juan_Esteban_Valencia

What does the last column contain? Can you explain how to interpret the numbers in the last column? Which number is larger and which is smaller? Manually, how the addition is done on such type of numbers?

Of course, the information in the last column contains the value of the sale that made the line, so in the linq, the idea is to obtain the list of lines in the order of which sold more to the one that sold less, But since there are lines that make more than one sale, the sales of each line must be added to know which one sold the most

@Juan_Esteban_Valencia

How to manually add these types of numbers. There are 3 decimal points.

Is it a currency or what

ah sorry, in Colombia the “.” is the thousands separator and the “,” is for decimals

@Juan_Esteban_Valencia

Proceed like this

Input Excel File

image

image

The LINQ used is


(From row In inputDT.AsEnumerable.Where(Function (r) r("Sociedad").ToString.Equals("Ind Colombiana Cafe S.A.S")) 
Group row By lv=row("LINEA NUEVA").ToString 
Into grp=Group 
Select outDT.Rows.Add(New Object(){grp(0)("Sociedad"), 
	lv, 
	grp.Sum(Function (s) CDbl(s("Ventas Netas Act").ToString.Replace(".","").Replace(",","."))).ToString("N0", Globalization.CultureInfo.CreateSpecificCulture("es-CO"))})
	).AsEnumerable.OrderByDescending(Function (r) CDbl(r("Ventas Netas Act").ToString.Replace(".","").Replace(",","."))).CopyToDataTable


For you reference

Order By in LINQ.xaml (6.0 KB)

1 Like