gpt4 book ai didi

Linq-按StartsWith排序,然后包含

转载 作者:行者123 更新时间:2023-12-04 09:37:08 25 4
gpt4 key购买 nike

假设我有3个客户名称:

Microsoft
Another customer also called Microsoft
A third customer called Microsoft

现在,如果我这样查询客户...
var q = (from cust in db.Cust
where cust.Name.Contains("Microsoft")
orderby cust.Name ascending
select cust)

...我得到这个命令:
A third customer called Microsoft
Another customer also called Microsoft
Microsoft

我想要的是首先获得Microsoft,这是基于它以“Microsoft”开头的事实。

将Contains更改为StartsWith当然会使我得到1个结果,而不是3个结果。

可以在单个查询中完成吗?

最佳答案

也许

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

关于Linq-按StartsWith排序,然后包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8004407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com