0

How to use IN clause having a list as reference?

List<PostOrcamentoServico> lOrcamentos = db.PostOrcamentoServico.Where(o => !o.Post.Usuarios.UsuEmail.Contains(User.Identity.Name) &&
                                                                       usuario.Clientes.Servicos.Contains(o.Servicos))
                                                                       .ToList();

in the above example, o.Servicos is a list.

Resuming: What I dould like to know how to do is use IN, however, using a list as reference:

myList.Contains(anotherList)
Dan
  • 1,518
  • 5
  • 20
  • 48
  • possible duplicate of [LINQ to SQL in and not in](http://stackoverflow.com/questions/3047657/linq-to-sql-in-and-not-in) – Arcturus Aug 21 '15 at 14:20
  • @Arcturus No, it isn't what I am looking for... I want a List as reference, and I am using Lambda not Linq – Dan Aug 21 '15 at 14:22
  • The lambda syntax and the query syntax are convertible and interchangeable. Having said that, it sounds like you're not simply looking to ensure a single value is contained in the list but rather the entire list is contained within the larger set. Is that correct? Your assertion that you "want a List as reference" isn't really clear. – Craig W. Aug 21 '15 at 14:26
  • @CraigW. You are correct. How can I improve my title? – Dan Aug 21 '15 at 14:28
  • 2
    possible duplicate of [Linq query list contains a list](http://stackoverflow.com/questions/2364090/linq-query-list-contains-a-list) – Dan Aug 21 '15 at 14:31

3 Answers3

2

From memory, you can do :

bool b = anotherList.All(x=> myList.Contains(x));

[EDIT] here a full test sample:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> l1 = new List<string>();   

            l1.Add("toto");l1.Add("titi") ;l1. Add("tata") ;l1.Add("tutu") ;l1.Add("tete");

            List<string> l2 = new List<string>();
            l2.Add("toto"); l2.Add ("titi"); l2.Add ( "tata") ;

            if (l2.All(l1.Contains))
            {
                System.Console.WriteLine("OK");
            }
            else 
            {
                System.Console.WriteLine("KO");
            }

        }
    }
}
Fabienne B.
  • 355
  • 1
  • 7
  • {"Unable to create a constant value of type 'Servicili.Models.Servicos'. Only primitive types or enumeration types are supported in this context."} – Dan Aug 21 '15 at 15:34
  • If you are using custom object into your lists you should override `Equals(Object obj)` in order to make `Contains` work. – Fabienne B. Aug 24 '15 at 08:40
0

use this :

bool contained = !subset.Except(superset).Any();

reference :

Determine if a sequence contains all elements of another sequence using Linq

Community
  • 1
  • 1
sm_
  • 2,572
  • 2
  • 17
  • 34
0

the right answer is this:

First: I get a list of items to compare:

var servicios = usuario.Clientes.Servicos.Select(s => s.SerId);

then I use this list with values to compare in my IN:

List<PostOrcamentoServico> lOrcamentos = db.PostOrcamentoServico.Where(os => !os.Post.Usuarios.UsuEmail.Contains(User.Identity.Name)&&
                                                                                      os.Servicos.All(s => servicios.Contains(s.SerId))).ToList();
Dan
  • 1,518
  • 5
  • 20
  • 48