需要一个LINQ查询来查找字符串项 [英] Need a LINQ query to find string items

查看:89
本文介绍了需要一个LINQ查询来查找字符串项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List<NameClass>,用于存储NameClass项的集合,该类具有名为Name的属性.我想做的是编写一个Linq查询,该查询将拉出所有以Jones开头的名称,但前提是出现3次或多次.例如,如果我的列表中包含以下项目:

I have a List<NameClass> that stores a collection of NameClass items with a property called Name in the class. What I'm trying to do is write a Linq query that will pull all the names that start with Jones, but only if there are 3 or more occurrences. For example, if my list had the following items:

Name
-----------
Jones
Jonestown
Smith
Hector
Jones
Smith
Smith

我正在寻找一个可以这样调用的C#函数:

I am looking for a C# function that I can call like this:

GetNames("Jones");

它应该返回:

Jones
Jonestown
Jones

如果我运行它:

GetNames("Smith");

它应该返回:

Smith
Smith
Smith

如果我运行它:

GetNames("Hector");

由于Hector不在列表中3次或多次,因此它不应返回任何内容.

It should return nothing since Hector isn't in the list 3 or more times.

任何帮助编写此LINQ查询的人将不胜感激!

Any help writing this LINQ query would be appreciated!

推荐答案

string searchString = "Jones";
string lowerSS = searchString.ToLower();

List<NameClass> nameClasses; 

var results = nameClasses.Where(nc => nc.Name.ToLower().StartsWith(lowerSS));

if(results != null && results.Count() >= 3)
{
    return results;
}
else
{
    return null;
}

这篇关于需要一个LINQ查询来查找字符串项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆