这是我当前的 LINQ 查询:
var model =
db.RPTINQUIRies
.Where(t => t.CONNAME.StartsWith(term))
.Take(25)
.Select(t => new
{
label = t.CONNAME
}).Distinct();
CONNAME 指的是联系人姓名,例如“Andy Smith”。我选择不同,因为可能有 100 行包含“Andy Smith”。
假设我有一些不同的名字如下:
- 安迪·史密斯
- 安迪博格斯
- 安迪道斯
- 安迪萨顿
如果我将术语“Andy”传递到查询中,我希望看到上面的列表,但它不会生成所有列表。它可能会生成 Andy Smith、Andy Bloggs,而缺少 Andy Dawes 和 Andy Sutton。如果我传递术语“Andy Su”,它将按预期显示 Andy Sutton。
谁能解释为什么会这样?
最佳答案
尝试移动 Take(25)
var model =
db.RPTINQUIRies
.Where(t => t.CONNAME.StartsWith(term))
.Select(t => new
{
label = t.CONNAME
})
.Distinct()
.Take(25); // Move it HERE!
您需要 25 个不同的名称,而不是 SQL 在数据库中找到的前 25 个名称的不同名称:-)
为了清楚起见:假设您有一个Take(2)
:
您对数据库所有行的查询...
Andy Smith
Andy Smith
Andy Bloggs
Andy Dawes
然后你Take(2)
...
Andy Smith
Andy Smith
并应用 Distinct()
...
Andy Smith
但事实上你想要:
获取数据库的所有行
Andy Smith
Andy Smith
Andy Bloggs
Andy Dawes
应用 Distinct()
Andy Smith
Andy Bloggs
Andy Dawes
然后取(2)
Andy Smith
Andy Bloggs
关于c# - C# 中 LINQ 查询的奇怪结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29097806/