1

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 }
Juanker
  • 764
  • 1
  • 9
  • 20
  • I want to get all objects who has all the admited types in it, but they can have more types on them, – Juanker Dec 19 '18 at 10:10
  • Possible duplicate of [Check whether an array is a subset of another](https://stackoverflow.com/questions/332973/check-whether-an-array-is-a-subset-of-another) – mjwills Dec 19 '18 at 10:11
  • You code is working properly. Add ToList() so you can see all the results : var objectsAdmited = allObjects.Where(b => b.Types.All(t => typesAdmited.Contains(t))).ToList(); – jdweng Dec 19 '18 at 10:38

2 Answers2

2

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

var objectsAdmited = allObjects.Where(b => typesAdmited.All(t => b.Types.Contains(t)));
Mohammad Karimi
  • 387
  • 2
  • 8
1

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
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69