Juanker
Juanker

Reputation: 794

c# Linq - Get all elements that contains a list of integers

I have a list of Objects with a list of types inside of each Object, somthing very similar like this:

public class ExampleObject
{
    public int Id {get; set;}
    public IEnumerable <int> Types {get;set;}
}

For example:

var typesAdmited = new List<int> { 13, 11, 67, 226, 82, 1, 66 };

And inside the list of Object I have an object like this:

Object.Id = 288;
Object.Types = new List<int> { 94, 13, 11, 67, 254, 256, 226, 82, 1, 66, 497, 21};

But when I use linq to get all Object who has the types admited I get any results. I am trying this:

var objectsAdmited = objects.Where(b => b.Types.All(t => typesAdmited.Contains(t)));

Example:

var typesAdmited = new List<int> { 13, 11, 67, 226, 82, 1, 66 };

var objectNotAdmited = new ExampleObeject {Id = 1, Types = new List<int> {13,11}}; 
var objectAdmited = new ExampleObject {Id = 288, Types = new List<int> { 94, 13, 11, 67, 254, 256, 226, 82, 1, 66, 497, 21}};

var allObjects = new List<ExampleObject> { objectNotAdmited, objectAdmited };

var objectsAdmited = allObjects.Where(b => b.Types.All(t => typesAdmited.Contains(t)));

I get:

objectsAdmited = { }

And it should be:

objectsAdmited = { objectAdmited }

Upvotes: 1

Views: 5789

Answers (2)

Patrick Artner
Patrick Artner

Reputation: 51643

You can solve this using Linq. See the small code block in the middle - the rest is boilerplate to make it a Minimal complete verifyabe answer:

using System;
using System.Collections.Generic;
using System.Linq; 

public class ExampleObject
{
  public int Id { get; set; }
  public IEnumerable<int> Types { get; set; }
}

class Program
{
  static void Main (string [] args)
  {
    var obs = new List<ExampleObject>
    {
      new ExampleObject
      {
        Id=1,
        Types=new List<int> { 94, 13, 11, 67, 254, 256, 226, 82, 1, 66, 497, 21 } 
      },
      new ExampleObject
      {
        Id=288,
        Types=new List<int> { 94, 13, 11, 67,      256, 226, 82, 1, 66, 497, 21 } 
      },
    };

    var must_support = new List<int>{11, 67, 254, 256, 226, 82, };  // only Id 1 fits

    var must_support2 = new List<int>{11, 67, 256, 226, 82, };      // both fit

    // this is the actual check: see for all objects in obs 
    // if all values of must_support are in the Types - Listing
    var supports  = obs.Where(o => must_support.All(i => o.Types.Contains(i)));
    var supports2 = obs.Where(o => must_support2.All(i => o.Types.Contains(i)));

    Console.WriteLine ("new List<int>{11, 67, 254, 256, 226, 82, };");
    foreach (var o in supports)
      Console.WriteLine (o.Id);

    Console.WriteLine ("new List<int>{11, 67, 256, 226, 82, };");
    foreach (var o in supports2)
      Console.WriteLine (o.Id);

    Console.ReadLine ();
  }
}

Output:

new List<int>{11, 67, 254, 256, 226, 82, };
1
new List<int>{11, 67, 256, 226, 82, };
1
288

Upvotes: 1

Mohammad Karimi
Mohammad Karimi

Reputation: 457

You have to change both lists in your LINQ query interchangeably:

var objectsAdmited = allObjects.Where(b => typesAdmited.All(t => b.Types.Contains(t)));

Upvotes: 2

Related Questions