ASP.NET MVC2的LINQ Where子句使用StartsWith [英] ASP.NET MVC2 Linq Where Clause using StartsWith
问题描述
我有我的观点的人不在少数过滤器,第一个是列表中第一个名字,姓氏,当其中一个选项被选中然后用户可以选择A,B,C ... X,Y公司名称, Z到只显示人们开始以选定字母。
如果(集合[过滤器] ==2){
presentations = presentations.Where(X => x.Person.FirstName.StartsWith(A));
presentations = presentations.OrderBy(X => x.Person.FirstName);
}
返回结果类似于
约翰Squirel
罗勒Boywiz
大卫·史密斯
这似乎并没有工作,我错过什么?
我挖远一点,这就是导致问题的查询。
SELECT [T0]。[说明],[T0]。[事件ID],[T0]。[ID],[T0]。[presentedOn]
[T0]。[弹头],[T0]。[SpeakerId],[T0]。[标题],[T0] [URL]
FROM [presentations] AS T0
LEFT OUTER JOIN [扬声器]为T1 ON([T1]。[ID] = [T0]。[ID])
WHERE([T1] [名字] LIKE'B'+'%')
ORDER BY [T1] [名字]
我已成功地解决问题。如果你看一下查询行
LEFT OUTER JOIN [扬声器]为T1 ON([T1]。[ID] = [T0]。[ID])
应该读
LEFT OUTER JOIN [扬声器]为T1 ON([T1]。[ID] = [T0]。[SpeakerId])
不明白为什么这虽然发生,任何人都可以看到如何解决这个问题?
I have a few filters on my view, the first one is list by first name, last name and company name when one of these options are selected the user can then select a, b, c ... x, y, z to show only people starting with the selected letter.
if (collection["Filter"] == "2") {
presentations = presentations.Where(x => x.Person.FirstName.StartsWith("A"));
presentations = presentations.OrderBy(x => x.Person.FirstName);
}
Results returned are similar to
John Squirel
Basil Boywiz
David Smith
This doesn't seem to work, what am I missing?
I dug a little further, this is the query causing the problem.
SELECT [t0].[Description], [t0].[EventId], [t0].[Id], [t0].[PresentedOn],
[t0].[Slug], [t0].[SpeakerId], [t0].[Title], [t0].[Url]
FROM [Presentations] AS t0
LEFT OUTER JOIN [Speakers] AS t1 ON ([t1].[Id] = [t0].[Id])
WHERE ([t1].[FirstName] LIKE 'B' + '%')
ORDER BY [t1].[FirstName]
I have managed to resolve the problem. If you look at the query the line
LEFT OUTER JOIN [Speakers] AS t1 ON ([t1].[Id] = [t0].[Id])
should read
LEFT OUTER JOIN [Speakers] AS t1 ON ([t1].[Id] = [t0].[SpeakerId])
Not quite sure why this is happening though, can anyone see how to correct this problem?
这篇关于ASP.NET MVC2的LINQ Where子句使用StartsWith的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!