LINQ (for entities) - Filtering elements using ints

Using LINQ to Entities, how can I determine if there is any element from an int list in a comma-delimited string?

For example, I want to write something like the following (logically):

collection.Where(collection.DelimitedStringOfInts.Contains(listOfInts.AnyOfThem))

Also, I should mention that I am doing this using the LINQ method chain, with a separator string as part of the object -

var listOfInts = GetListOfInts();
var query = from x in Db.Items select x;
if (listOfInts != null && listOfInts.Count() > 0)
{
  query = query.Where(x => x.DelimitedStringOfInts.Contains(listOfInts.AnyOfThem));
}

UPDATE:
Using an article linking to Alex, I implemented a working solution as follows:

var query = from x in Db.Items select x;
var listOfInts = GetListOfInts();
if (listOfInts != null && listOfInts.Any())
{
    //"ToListOfStrings" is a simple extension method I wrote to create a List<string> from a List<int>
    var delimitedIds = listOfInts.ToListOfStrings(','); 
    query = query.Where(
        BuildOrExpression<DatabaseItem, string>(x => x.DelimitedStringOfInts, delimitedIds)
        );
}

The BuildOrExpression referred to in the article requires an update. The equal operator should have been replaced by the contains operator.

var contains = values.Select(value =>
    (Expression)Expression.Call(
                   valueSelector.Body,
                   typeof(string).GetMethod("Contains"),
                   Expression.Constant(
                           value,
                           typeof(TValue)
                   )
           )
   );
+3
source share
6 answers

tip, , , , , .

+5
DelimitedStringOfInts.Split(new char[]{','})
                     .Select<string, int>(s => int.Parse(s))
                     .Intersect(listOfInts).Count<int>() > 0
+4

HashSet .Contains. .Any() true, .

 var stringofInts = "2,3,5,9";
 List<int> listOfInts = GetSomeListOfInts();

 var set = new HashSet<int>(stringofInts.Split(',').Select(x => int.Parse(x)));
 listOfInts.Any(x => set.Contains(x))
+2

, SQL, EF .

, :

query = query.Select(x => x.DelimitedStringOfInts)
            .ToList() // Run the query in the database now
            .Where(ints => ints.Select(s => int.Parse(s)).Any(listOfInts.Contains));

, , sprocs raw SQL , Alex .

HashSet .

And, as another idea (I don’t know if you can do this), but if there is the possibility of extending EF QueryProvider, this would make it possible to transfer the call List.Containsto SQL WHERE ... IN .... Maybe not easy. By the way, EF 4 will have a built-in interface.

+1
source

Try the following:

int i = 0;

bool exists = StringOfInts.Split(',').Any(
                s => Int32.TryParse(s, out i) && 
                     listOfInts.Any(n => n == i)
              );
0
source

This will do the trick ...

 List<int> integerList = new List<int>() { 1, 2, 3, 4 };
 string integersAsString = "1,3,5";


 var result = integersAsString.Split(',')
                             .Select(s => Int32.Parse(s))
                             .Where(i => integerList.Contains(i));

 foreach (var i in result)
    Console.WriteLine(i); // prints 1 and 3
0
source

Source: https://habr.com/ru/post/1721935/


All Articles