1

this is my query in 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 c#

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
                });

enter image description here the name "len" does not exist in the real context

only this query works

var product = (from d in db.DetalleNotas
                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
6
  • 3
    "does not work" in what way does it not work? Do you see errors? Commented May 4, 2018 at 15:58
  • 1
    mi error is the name "len" does not exist in the real context Commented May 4, 2018 at 16:08
  • Possible duplicate of LINQ to SQL - Checking the field length Commented May 4, 2018 at 16:14
  • 2
    You need ColProduct.Length instead of len Commented May 4, 2018 at 16:33
  • @Seano666 not, mi question is deferent, Commented May 4, 2018 at 16:37

2 Answers 2

5

Remember that in C# Linq code, everything is still C#. As you're working with strings you need to order by string.Length. For example:

var results = from d in db.DetalleNotas
              orderby d.ColProduct.Length
              select d;
Sign up to request clarification or add additional context in comments.

Comments

5

You could order it by the number on the end of your strings:

var result = db.DetalleNotas.OrderBy(d => Convert.ToInt32(d.ColProduct.Substring(5)))
                            .ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.