Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

check list exist in list c# if matches any

List<int> nums1 = new List<int> { 2, 4, 6, 8, 10 };
List<int> nums2 = new List<int> { 1, 3, 6, 9, 12};

if (nums1.Any(x => nums2.Any(y => y == x)))
{
    Console.WriteLine("There are equal elements");
}
else
{
    Console.WriteLine("No Match Found!");
}
Comment

check if value in list c#

// 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));
    }
}
Comment

c# 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;
	}
}
Comment

check list exist in list c# if matches any

var items = (from x in parameters
                join y in myStrings on x.Source equals y
                select x)
            .ToList();
Comment

check list exist in list c# if matches any

You could use a nested Any() for this check which is available on any Enumerable:

bool hasMatch = myStrings.Any(x => parameters.Any(y => y.source == x));
Faster performing on larger collections would be to project parameters to source and then use Intersect which internally uses a HashSet<T> so instead of O(n^2) for the first approach (the equivalent of two nested loops) you can do the check in O(n) :

bool hasMatch = parameters.Select(x => x.source)
                          .Intersect(myStrings)
                          .Any(); 
Comment

c# 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...
");
      foreach(string list in list1) {
         Console.WriteLine(list);
      }

      Console.Write("Finding an item in the list...
");
      if (list1.Contains("Adams") == true) {
         Console.WriteLine("Item exists!");
      } else {
         Console.WriteLine("Item does not exist!");
      }
   }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# create log file 
Csharp :: .net 4.5 use tls 1.2 
Csharp :: get mouse inpuit new input system 
Csharp :: unity audio source 
Csharp :: vb.net datagridview set row index 
Csharp :: c# check if object is of any generic type 
Csharp :: group-by-in-linq 
Csharp :: select distinct linq mvc 
Csharp :: c# clear console read chache 
Csharp :: c# read xml tag value 
Csharp :: how to write web service for API in c# 
Csharp :: how to change text in richtextbox wpf 
Csharp :: c# caractère cacher mot de passe 
Csharp :: start a particle effect when a button is pushed 
Csharp :: upload a file selenium c# 
Csharp :: multiply structs c# 
Csharp :: Send Hotmail/Outlook Email C# (Win/ASP.NET) 
Csharp :: unity rigidbody freeze all rotation 
Csharp :: irrrtate throught an matrix c# 
Csharp :: How to decode Microsoft Local token in service 
Csharp :: install nuget package for S3 
Csharp :: c# code to check anagram 
Csharp :: how to find the biggest number in c# 
Csharp :: C# get filebase name 
Csharp :: c# convert securestring to string 
Csharp :: C# random multiple of 5 in range 
Csharp :: select top 5 in linq c# 
Csharp :: instantiate date time variable C# 
Csharp :: c# mysql select into datatable 
Csharp :: c# comments 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =