Comparing strings in 2 list using Linq

c# linq compare two lists of objects
how to filter two list in c# using linq
compare two list in c# using linq and update value
c# compare two lists of different objects
c# compare two lists for matches
linq contains list
c# list compare items
find uncommon elements in two lists c#

I need to compare to list with strings, and find whitch elements that are similar. ie.

List<string> listA = {"ProfileHeight", "ProfileWidth", "WebThickness"}
List<string> listB ={"ProfileHeightVisibility", "ProfileWidthVisibility", "WebThicknessVisibility", "FlangeThicknessVisibility", "DepthVisibility"}

I was wondering if it is possible to use Linq. I want the result to be a list with the elements from ListB that have a name similar to the strings in listA. Where the statemant i.e. "ProfileHeightVisibility.Contains("ProfileHeight") = true

You mean you want items in listB where it contains any of the elements in listA?

listB.Where(b => listA.Any(a => b.Contains(a))

This example shows how to use LINQ to compare two lists of strings and output those lines that are in names1.txt but not in names2.txt. This example shows how to use LINQ to compare two lists of strings and output those lines that are in names1.txt but not in names2.txt. To create the data files Copy names1.txt and names2.txt to your solution folder as shown in How to combine and compare string collections (LINQ) (C#) .

You can do it in LINQ:

listB.Where(e => listA.Any(a => e.Contains(a)));

Bear in mind that this algorithm has a runtime cost similar to O(n^2), it will therefore slow down quickly as the number of elements in your lists grows.

Instead of arrays we can compare two lists using the SequenceEqual() method.So if there are two lists of strings declared as: List<string> arr4 = new List<string>(){ "A", "B", "C" }; List<string> arr5 = new List<string>() { "A", "B", "C" }; then we can compare them as: bool result = arr4. To compare lists using LINQ there is a SequenceEqual extension method provided by the framework.If we have two arrays arr1 and arr2 declared as: string [] arr1 = { "A", "B", "C" }; string [] arr2 = { "A", "B", "C" };

You can also implement things like that :

  public class EqualityComparer : IEqualityComparer<string>
  {

    public bool Equals(string x, string y)
    {
      return y.Contains(x);
    }

    public int GetHashCode(string obj)
    {
      return 1;
    }
   }

and then use intersect like that:

 listB.Intersect(listA, new EqualityComparer()).ToList();

Based on example provided by OriginalGriff[^], i'd share another solution, which uses Where + Any Linq methods: Hide Copy Code. var found  I need to compare and get the matching values from a string list with LINQ. Have a look at my code. Code. Split = Id.Split(','); List<string> uids = new List<string>(Split); var model = (from xx in Db.ItemWeedLogs where xx.ItemNo == uids // I need to pass a string list to extract the matching record.

Yes you can do such thing with LINQ

    public List<string> GetSimilarStrings(List<string> list1, List<string> list2)
    {
        return (from item in list1 from item2 in list2 where string.Equals(item,item2,StringComparison.InvariantCultureIgnoreCase) select item).ToList();
    }

Here are two different ways of doing it: 1. Standard Query. Hide Copy Code. var resultantList = from item1 in list1 join item2 in list2 on item1. Yes you can do such thing with LINQ. public List<string> GetSimilarStrings(List<string> list1, List<string> list2) { return (from item in list1 from item2 in list2 where string.Equals(item,item2,StringComparison.InvariantCultureIgnoreCase) select item).ToList(); } share. |.

List<string> listA = new List<string> { "zinmin", "zin1", "zin2" };
List<string> listB = new List<string> { "zinmin", "min1", "min2" };



List<string> matching = listA.Intersect(listB).ToList();

matching result will come {"zinmin"} . Sorting order doesn't matter.

Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < string > list1 = new List < string > (); list1.Add("P");  it should compare each character and it should not consider if the character is X. for the above two strings the result is true. now i'm using like. Iterating through the length of the string and replacing thecorresponding character in first string with X. is there any way to do this using LINQ

Second List contains ID's and ID Name I want to check if ALL ID's in First List //​Sample Class here public class Person { private string name; private One more thing, I dont want to return all the missing elements from List 2. None of the samples have used ReferenceEquals. This method determines if two strings are the same object. This can lead to inconsistent results in string comparisons. The following example demonstrates the string interning feature of C#. When a program declares two or more identical string variables, the compiler stores them all in the same

Compare two List<string> using LINQ. In this post, we will see how to compare two List<string> and list the elements found in one List<string> but not in the other  String equality comparison using many parameters When many string parameter values must be matched fully against a single column, the SQL IN operator can be used. To generate this operator, a parameter of type ICollection<string> can be used (manyParam) with the ICollection<string>.Contains(string) method.

SequenceEqual loops through the 2 lists and compares each item to two lists of strings don't match List<string> strings1 = new List<string>();  How to combine and compare string collections (LINQ) (C#) 07/20/2015; 2 minutes to read +7; In this article. This example shows how to merge files that contain lines of text and then sort the results. Specifically, it shows how to perform a simple concatenation, a union, and an intersection on the two sets of text lines.

Comments
  • And by "similar" you mean StartsWith or Contains?
  • Why do you use string.Compare? This method returns an integer stating the lexical order of two strings rather than a boolean, does it not?
  • Yeah, good point. I corrected my answer. Comparing strings by "==" could cause problems with culture, encoding or case sensitiveness of words. This example shows how to handle those things.
  • I'm not sure whether the strings are supposed to be equal. However, I'd maybe rather use string.Equals(a, b, StringComparision.InvariantCultureIgnoreCase) msdn.microsoft.com/en-us/library/t4411bks.aspx
  • You are right, it could be even better to use String.Equals.