csharp does value exist in list
Code Example - csharp does value exist in list
public int GetItemFromList() {
List<Item> list = new List<Item>(
new Item(1),
new Item(2),
new Item(3)
);
Item testItem = new Item(1);
// Inside FindIndex() you can specify a lambda expression where you
// query if an item exists like a boolean test.
int index = list.FindIndex(item => testItem.Id == item.Id);
// in this case out testItem.Id (1) is equal to an item in the list
if (index > -1)
{
// We get here with the index 0!
return index;
}
}
public class Item
{
public int Id { get; set; }
public Item() { }
public Item(int id)
{
Id = id;
}
}
check if value in list csharp
// C# Program to check whether the
// element is present in the List
// or not
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// Creating an List<T> of Integers
List<int> firstlist = new List<int>();
// Adding elements to List
firstlist.Add(1);
firstlist.Add(2);
firstlist.Add(3);
firstlist.Add(4);
firstlist.Add(5);
firstlist.Add(6);
firstlist.Add(7);
// Checking whether 4 is present
// in List or not
Console.Write(firstlist.Contains(4));
}
}
check list exist in list csharp if matches any
var items = (from x in parameters
join y in myStrings on x.Source equals y
select x)
.ToList();
csharp exists in list
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List < string > list1 = new List < string > () {
"Lawrence",
"Adams",
"Pitt",
"Tom"
};
Console.Write("List...\n");
foreach(string list in list1) {
Console.WriteLine(list);
}
Console.Write("Finding an item in the list...\n");
if (list1.Contains("Adams") == true) {
Console.WriteLine("Item exists!");
} else {
Console.WriteLine("Item does not exist!");
}
}
}