猿问
下载APP

在LINQ中找到List中的项目?

这里我有一个简单的例子来查找字符串列表中的项目。通常我使用for循环或匿名委托来这样做:


int GetItemIndex(string search)

{

   int found = -1;

   if ( _list != null )

   {

     foreach (string item in _list) // _list is an instance of List<string>

     { 

        found++;

        if ( string.Equals(search, item) )

        {

           break;

        }

      }

      /* use anonymous delegate

      string foundItem = _list.Find( delegate(string item) {

         found++;

         return string.Equals(search, item);

      });

      */

   }

   return found;

}

LINQ对我来说是新的。我很好奇我是否可以使用LINQ来查找列表中的项目?怎么可能?


米琪卡哇伊
浏览 59回答 3
3回答

慕运维8079593

有几种方法(注意这不是一个完整的清单)。1)Single将返回一个结果,但如果找不到或多于一个(可能是你想要的或不是你想要的),将抛出异常:string search = "lookforme";List<string> myList = new List<string>();string result = myList.Single(s => s == search);注意SingleOrDefault()将表现相同,除了它将为引用类型返回null,或者为值类型返回默认值,而不是抛出异常。2)哪里将返回符合您条件的所有项目,因此您可以获得一个元素的IEnumerable:IEnumerable<string> results = myList.Where(s => s == search);3)首先将返回符合您标准的第一项:string result = myList.First(s => s == search);注意FirstOrDefault()将表现相同,除了它将为引用类型返回null,或者为值类型返回默认值,而不是抛出异常。

慕标琳琳

如果你想要元素的索引,这将做到:int index = list.Select((item, i) => new { Item = item, Index = i })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .First(x => x.Item == search).Index;// orvar tagged = list.Select((item, i) => new { Item = item, Index = i });int index = (from pair in tagged&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where pair.Item == search&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select pair.Index).First();你不能在第一关中摆脱lambda。请注意,如果该项目不存在,则会抛出此值。这通过诉诸可空的int来解决问题:var tagged = list.Select((item, i) => new { Item = item, Index = (int?)i });int? index = (from pair in tagged&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where pair.Item == search&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select pair.Index).FirstOrDefault();如果你想要这个项目:// Throws if not foundvar item = list.First(item => item == search);// orvar item = (from item in list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where item == search&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select item).First();// Null if not foundvar item = list.FirstOrDefault(item => item == search);// orvar item = (from item in list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where item == search&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select item).FirstOrDefault();如果要计算匹配的项目数:int count = list.Count(item => item == search);// orint count = (from item in list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where item == search&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select item).Count();如果您想要匹配的所有项目:var items = list.Where(item => item == search);// orvar items = from item in list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where item == search&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select item;并且不要忘记null在任何这些情况下检查列表。或者用(list ?? Enumerable.Empty<string>())而不是list。

噜噜哒

如果真的是List<string>你不需要LINQ,只需使用:int GetItemIndex(string search){&nbsp; &nbsp; return _list == null ? -1 : _list.IndexOf(search);}如果您正在寻找物品本身,请尝试:string GetItem(string search){&nbsp; &nbsp; return _list == null ? null : _list.FirstOrDefault(s => s.Equals(search));}
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答