这里我有一个简单的例子,在字符串列表中查找一个项。通常我使用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 an anonymous delegate
      string foundItem = _list.Find( delegate(string item) {
         found++;
         return string.Equals(search, item);
      });
      */
   }
   return found;
}

LINQ对我来说是新的。我可以使用LINQ在列表中查找项目吗?如果这是可能的,怎么做?


当前回答

试试下面的代码:

return context.EntitytableName.AsEnumerable().Find(p => p.LoginID.Equals(loginID) && p.Password.Equals(password)).Select(p => new ModelTableName{ FirstName = p.FirstName, UserID = p.UserID });

其他回答

如果它确实是一个List<string>,你不需要LINQ,只需使用:

int GetItemIndex(string search)
{
    return _list == null ? -1 : _list.IndexOf(search);
}

如果你正在寻找物品本身,试试:

string GetItem(string search)
{
    return _list == null ? null : _list.FirstOrDefault(s => s.Equals(search));
}

如果你想要元素的索引,这将做到:

int index = list.Select((item, i) => new { Item = item, Index = i })
                .First(x => x.Item == search).Index;

// or
var tagged = list.Select((item, i) => new { Item = item, Index = i });
int index = (from pair in tagged
            where pair.Item == search
            select pair.Index).First();

你不能在第一遍就消去。

注意,如果项目不存在,这将抛出。这通过使用可空int型来解决问题:

var tagged = list.Select((item, i) => new { Item = item, Index = (int?)i });
int? index = (from pair in tagged
            where pair.Item == search
            select pair.Index).FirstOrDefault();

如果你想要这个项目:

// Throws if not found
var item = list.First(item => item == search);
// or
var item = (from item in list
            where item == search
            select item).First();

// Null if not found
var item = list.FirstOrDefault(item => item == search);
// or
var item = (from item in list
            where item == search
            select item).FirstOrDefault();

如果您想统计匹配项的数量:

int count = list.Count(item => item == search);
// or
int count = (from item in list
            where item == search
            select item).Count();

如果你想要所有匹配的项:

var items = list.Where(item => item == search);
// or
var items = from item in list
            where item == search
            select item;

不要忘记在这些情况下检查列表是否为空。

或者使用(list ??Enumerable.Empty<string>())而不是list。

IndexOf怎么样?

搜索指定对象并返回列表中第一个出现项的索引

例如

> var boys = new List<string>{"Harry", "Ron", "Neville"};  
> boys.IndexOf("Neville")  
2
> boys[2] == "Neville"
True

注意,如果该值没有出现在列表中,则返回-1

> boys.IndexOf("Hermione")  
-1

如果我们需要从列表中找到一个元素,那么我们可以使用find和FindAll扩展方法,但是它们之间有细微的区别。这里有一个例子。

 List<int> items = new List<int>() { 10, 9, 8, 4, 8, 7, 8 };

  // It will return only one 8 as Find returns only the first occurrence of matched elements.
     var result = items.Find(ls => ls == 8);      
 // this will returns three {8,8,8} as FindAll returns all the matched elements.
      var result1 = items.FindAll(ls => ls == 8); 

你可以使用FirstOfDefault和Where LINQ扩展从IEnumerable中获取MessageAction类。芮米

var action = Message.Actions.Where(e => e.targetByName == className).FirstOrDefault<MessageAction>();

在哪里

List<MessageAction> Actions { get; set; }