order by len () in linq

3

This is my query on the sql server and everything works fine

select * from DetalleNotas
order by len(ColProduct), ColProduct

PROCT1
PROCT2
PROCT3
PROCT4
PROCT5
PROCT6
PROCT7
PROCT8
PROCT9
PROCT10

but I want my query in linq

I tried this and it does not work

var product = (from d in db.Product
                orderby len(d.ColProduct), d.ColProduct
                select new
                {
                    product= d.product
                });

only this query works

var product = (from d in db.Product
                orderby d.ColProduct
                select new
                {
                    product= d.product
                });

This is the result of my functional query

PROCT1
PROCT10
PROCT2
PROCT3
PROCT4
PROCT5
PROCT6
PROCT7
PROCT8
PROCT9
    
asked by Rodrigo Rodriguez 04.05.2018 в 17:46
source

1 answer

6

You may want to do the orderby for:

orderby d.ColProduct.Length

If d.ColProduct is of type string , below an example:

var s = new List<string> { "hola", "hello33", "hillos" };

var T = from e in s orderby e.Length select e;
var T2 = s.OrderBy(x => x.Length);

Console.WriteLine("Con query syntax: ");
foreach (var k in T) 
    Console.WriteLine(k);

Console.WriteLine("\nCon method syntax:");
foreach (var k in T2)
    Console.WriteLine(k);

The above example I did on a List<string> , but it is the same if you make the modification that I mentioned a moment ago, this is the result that throws me:

Con query syntax: 
hola
hillos
hello33

Con method syntax:
hola
hillos
hello33

Applying it to your query would be:

var product = (from d in db.Product
               orderby d.ColProduct.Length select new {
                 product= d.product
              });

Here you have a fiddle to try, greetings! :)

    
answered by 04.05.2018 / 18:14
source