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).
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.
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.
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!
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
Your code doesn't work as expected for two reasons.
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".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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With