Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq contains not working

Tags:

c#

linq

I am using following code to filter the list containing the string pre. What am i missing ? Here is the code

string pre = "a";
List<string> A = new List<string>();
List<string> B = new List<string>();
DAL dal = new DAL();
A = dal.GetNames();
B = (from x in A
     where A.Contains(pre)
     select x).ToList();
B = A.Where(c => A.Contains(pre)).ToList();

B is always coming empty here(in both cases).

like image 655
kkakroo Avatar asked Dec 28 '14 10:12

kkakroo


People also ask

Why is my LINQ contains method not working?

This is because the Linq Contains Method in C# does not check the values rather it checks the object reference and in this case, the object references are different. If you want to check the values rather than the reference then you need to create a class and need to implement the IEqualityComparere interface.

What is LINQ contains operator in SQL?

The LINQ Contains operator comes under the Quantifier operator category, the main purpose of this operator is used to check whether the specified element present in the collection or not, and finally it returns the boolean value as a result. LINQ Contains will not be supported in Query syntax it will be available only in the method syntax.

Is LINQ a good tool for writing code?

Linq is quite powerful, but can be very confusing (try doing a distinct). It is probably one of the best tools around since it saves so many lines of code. The content must be between 30 and 50000 characters. … Download, Vote, Comment, Publish. Forgot your password? This email is in use. Do you need your password? Submit your solution!


2 Answers

I guess what you want to do is to find all items in the list that contains the word pre? In that case, should change this:

B = (from x in A
     where A.Contains(pre)
     select x).ToList();

into

B = (from x in A
     where x.Contains(pre)
     select x).ToList();

Your Linq query only return non-empty result when there is an exact match of pre in the list

like image 195
Phuong Nguyen Avatar answered Nov 09 '22 15:11

Phuong Nguyen


Your code doesn't work as expected for two reasons.

  1. It sounds like you want to match any sub string of each list entry based on your search criteria (the variable pre), but you're searching currently for an exact match on each entry. Hence your current code returns nothing because the list A doesn't contain any string that is just "a".
  2. Your lambda expressions are probably wrong, as they refer to the entire instance of A rather than the current entry of A being evaluated.

To fix 1) instead of Contains, use IndexOf to assess each string in the list, so an exact match isn't required, it just looks for any match of the search criteria, anywhere in each string in the list.

To fix 2), understand that in the lambda expression, the variable on the left is referring to the current item being iterated on. So given a list x of 1,2,3,4,5, if my expression is n => n > 2, that means in the iteration this expression is evaluated 5 times, and n is either 1, 2, 3, 4, or 5. On each of these numbers, the expression is n > 2? is assessed, resulting in a true or false value for each entry in the list. In your code, you are not using the variable referring to the current list item being iterated on in the expression, so the evaluation has nothing to do with the current entry - all evaluations will return the same result, for example if I changed my original expression to n => 2 > 1, all items in the list evaluate to true for this expression.

Fixing this is simple, as shown in the other answers - in your expression, use the variable on the left hand side of the lambda expression instead of referring to the list outside which is at another scope.

string pre = "a";
List<string> A = new List<string>();
List<string> B = new List<string>();

A = new List<string>() { "a", "ba", "bb", "bc" };

B = (from x in A
        where x.IndexOf(pre) > -1
        select x).ToList();

B = A.Where(c => c.IndexOf(pre) > -1).ToList();
like image 20
steve16351 Avatar answered Nov 09 '22 14:11

steve16351