Linq - 按StartsWith排序然后包含

Mal*_*ako 7 linq

假设我有3个客户名称:

Microsoft
Another customer also called Microsoft
A third customer called Microsoft
Run Code Online (Sandbox Code Playgroud)

现在,如果我像这样查询客户......

var q = (from cust in db.Cust
                    where cust.Name.Contains("Microsoft")
                    orderby cust.Name ascending
                    select cust)
Run Code Online (Sandbox Code Playgroud)

...我得到这个订单:

A third customer called Microsoft
Another customer also called Microsoft
Microsoft
Run Code Online (Sandbox Code Playgroud)

我想要的是让微软第一,基于它以"微软"开头的事实.

将包含更改为StartsWith当然会留下1个结果而不是3个结果.

这可以在一个查询中完成吗?

Val*_*zub 17

也许

var q = (from cust in db.Cust
                    where cust.Name.Contains("Microsoft")
                    orderby cust.Name.IndexOf("Microsoft"),
                             cust.Name.Length ascending
                    select cust)
Run Code Online (Sandbox Code Playgroud)