简体   繁体   English

Linq查询过滤结果

[英]Linq Query filtering results

I'm not super familiar with LINQ. 我对LINQ不太熟悉。 Here's some code: 这是一些代码:

if (!String.IsNullOrEmpty(searchString))
            {
                games = games.Where(x => x.Name.Contains(searchString));
            }

Works as it should; 正常工作; however, what I'd like, rather than filtering results based on whatever text the user inputs, if the user inputs say "L" is there a way within the LINQ query to have it filter through the 1st letters of x.Name verses any x.Name that contains the letter. 但是,我想要的是,而不是根据用户输入的x.Name过滤结果,如果用户输入说“ L”,则LINQ查询中有一种方法可以使其通过x.Name第一个字母进行x.Name x.Name 。包含字母的名称。

IE say I have... IE说我有...

League_of_Legends
LOTRO_
Dota_2
Call_of_Duty_4
Final_Fantasy_7

And the user inputs "L" rather than returning all but Dota 2 , can it return only League of Legends and LOTRO as they are the only two who's names actually start with "L". 并且用户输入“ L”而不是返回除了Dota 2之外的所有内容,它可以仅返回League of LegendsLOTRO因为它们是仅有的两个实际上以“ L”开头的名字。

I'm assuming its something along the lines of... games = games.Where(x => x.Name.Contains<***something***>(searchString)); 我假设它类似于... games = games.Where(x => x.Name.Contains<***something***>(searchString));

Thanks! 谢谢!

I assume you're looking for StartsWith . 我假设您正在寻找StartsWith Note that this will work for any prefix, not just a single letter. 请注意,这将适用于任何前缀,而不仅仅是单个字母。

games = games.Where(x => x.Name.StartsWith(searchString));

If you want a case-insensitive search (eg where lowercase l matches League_of_Legends too), use: 如果要进行不区分大小写的搜索(例如,小写字母l匹配League_of_Legends ),请使用:

games = games.Where(x => x.Name.StartsWith(searchString, StringComparison.CurrentCultureIgnoreCase));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM