Linq Contains check on string had mis-detect

list.find c#
c# find string in list
c# list findindex
c# list contains
c# list findall
c# linq
c# remove duplicates from list of objects
c# list remove

I want to use LINQ to filter data from a string via LINQ, my expected result was only 1, 4, 15. but the sample show only 1, 4. it can't distinguish between 5 and 15.

could anyone give me some advice, thank you.

Here is .net fiddle

        string sa = "1, 2, 3, 4, 5, 15";
        string sr = "2, 3, 5";

        var r1 = sa.Where(l => !sr.Contains(l));
        var r2 = sa.Except(sr);

        var r3 = string.Join(",", sa.Except(sr));

        foreach (var i in r1)
        {
            Console.Write(i + ", ");
        }
        Console.WriteLine();

        foreach (var j in r2)
        {
            Console.Write(j + ", ");
        }
        Console.WriteLine();

        Console.WriteLine(r3);

The results show:

1, 4, 1, 
1, 4, 
1,4

Are you looking for Split?

var r3 => string.Join(", " sa
  .Split(new string[] { ", "}, StringSplitOptions.None)
  .Except(sr.Split(new string[] { ", "}, StringSplitOptions.None)));

We split each string into items:

"1, 2, 3, 4, 5, 15" -> 1, 2, 3, 4, 5, 15  
"2, 3, 5"           -> 2, 3, 5

then perform Except:

Except:                1, 4, 15

Finally, Join the string back from the items:

1, 4, 15            -> "1, 4, 15"

List<T>.Find(Predicate<T>) Method, Contains("seat"))); // Check if an item with Id 1444 exists. Xml.Linq; namespace Find { class Program { private static string IDtoFind = "bk109"; private static  When you need to match a fragment of a string with database values, you can use the String:Contains(string), String:StartsWith(string) and String:EndsWith(string)methods. All these methods will translate to SQL LIKE operations.

When you operate LINQ queries over a string, you are using it as an array, and are operating over the characters.

So, the LINQ queries never see 15 ever. They only see the character 5 and the character 1

Changing from strings, to int arrays, will allow you to pass the numbers to LINQ

The only change was where the numbers were defined.

string sa = "1, 2, 3, 4, 5, 15";
string sr = "2, 3, 5";

became

int[] sa = {1, 2, 3, 4, 5, 15};
int[] sr = {2, 3, 5};

Which changed the type of elements that LINQ was working over, from characters to ints.

Before you were also operating on the whitespace, and the comma's which you probably didn't want to do.

https://dotnetfiddle.net/kqjbDc

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        int[] sa = {1, 2, 3, 4, 5, 15};
        int[] sr = {2, 3, 5};

        var r1 = sa.Where(l => !sr.Contains(l));
        var r2 = sa.Except(sr);

        var r3 = string.Join(",", sa.Except(sr));

        foreach (var i in r1)
        {
            Console.Write(i + ", ");
        }
        Console.WriteLine();

        foreach (var j in r2)
        {
            Console.Write(j + ", ");
        }
        Console.WriteLine();

        Console.WriteLine(r3);

    }
}

result

1, 4, 15, 
1, 4, 15, 
1,4,15

Enumerable.Contains Method (System.Linq), Determines whether a sequence contains a specified element. true if the source sequence contains an element that has the specified value; otherwise, false . TSource) to determine whether an array contains a specific element. public class Product { public string Name { get; set; } public int Code { get; set; } } // Custom  In the call to Split, the punctuation marks are used as separators in order to remove them from the string. If you did not do this, for example you could have a string "Historically," that would not match "Historically" in the wordsToMatch array. You may have to use additional separators, depending on the types of punctuation found in the source text.

There is your Problem

 var r1 = sa.Where(l => !sr.Contains(l));

15 did contains the 5 so he cant select the 15.

use following Code to check for the complete number:

var r1 = sa.Where(l => sr.Any(f => f != l));

CSharp Driver LINQ Tutorial, You should already have read at least the CSharp Driver Tutorial introduction to the You can also mix and match the styles, which can be useful when using You use a projection to identify the field or property whose distinct values you want. to test whether a string field or property of the document contains a particular  Why don't you work on a list of int ? you can simply convert it to a list of int. because for string 15 contains 5 – Drag and Drop Jun 5 '18 at 7:23 and if you want to start by converting your string representation of a List int first , you can use Something like : Func<string, int?> myTryParse = s => int.TryParse(s, out int n) ?

Language Integrated Query (LINQ) and Lambda Expressions, This set includes operators to perform many different types of operations, such as From that collection, we would like to determine the list of people who logged in to string in the IEnumerable<string> to determine if the string has “logged in. Product: Chef Anton's Gumbo Mix Product: Louisiana Fiery Hot Pepper Sauce​  How to query for characters in a string (LINQ) (C#) 07/20/2015; 2 minutes to read +5; In this article. Because the String class implements the generic IEnumerable<T> interface, any string can be queried as a sequence of characters.

C# Remove Duplicates From List, The List contains duplicate elements for the values 3 and 4. Step 2: We use Linq; class Item { public int Key; public string Value; } class ItemEqualityComparer​  Say we have the following string list: If you'd like to check if a certain string element is present in the sequence then you can use the Contains operator in LINQ: This yields true as you might expect. This was a straightforward case as .NET has a good built-in implementation of string equality. It works…

Sorting data: the OrderBy() & ThenBy() methods, Fortunately for us, LINQ has several easy-to-use methods for sorting data But of course you can get your list of integers and strings sorted easily - that's a piece of cake! calls, in case your data is more complex than the data from our test case. And of course, you can mix and match the OrderBy(), OrderByDescending()​,  Check if a list contains a gameobject with a specified name. Check if a list contains a gameobject with a specified name. had just came up 4 days ago with Linq it

Comments
  • Why don't you work on a list of int ? you can simply convert it to a list of int. because for string 15 contains 5
  • and if you want to start by converting your string representation of a List int first , you can use Something like : Func<string, int?> myTryParse = s => int.TryParse(s, out int n) ? (int?)n : null; and filter the null out if some of the value are not int^^