c# - Using Contains method within linq's Where clause -


i've got problem query. have 2 simple classes. let's say

public class a{   public list<b> mycollection{get; set;} } public class b{  public string id; } //i want var myb = new b{id="1"}; context.a.where( x=> x.mycollection.contains(myb)).tolist(); 

how can solve this? know like

context.a.tolist().where... 

but that's not idea, have few thousands records.

update! context entityframework context , context.a represents dbset i'm still getting error "linq entities not recognize method 'boolean contains" can't use

context.a.tolist().where(.... 

because have thousands of records , inefficient

this worked me:

public class {     public list<b> mycollection{get; set;} }  public class b {      public string id; }  void main() {      // you're searching     var myb = new b{id="1"};       // here objects put in collection     a1 = new a();     a1.mycollection = new list<b>();     a2 = new a();     a2.mycollection = new list<b> { myb };     a3 = new a();     a3.mycollection = new list<b> { new b {id="1"}};       // here's list represents context.a     list<a> contexta = new list<a> {a1, a2, a3};       // here's actual search. results has count of 1     var results = contexta.where( x=> x.mycollection.contains(myb));     console.writeline(results.count());  } 

note finds a2, because literally put object "myb" in there. not find a3, new object created same id.

if wanted find both a2 , a3, you'd want change this:

var results = contexta.where( x=> x.mycollection.any(b => b.id == myb.id)); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -