Matt M.
Matt M.

Reputation: 822

Linq: the best overloaded match has some invalid arguments

I do a linq to sql query to get a list:

var list1 = db.Table1.Where(a => a.item == "Widgets").ToList();

Now I want to get a list from another table using the results of list above:

var list2 = db.Table2.Where(a => list1.Contains(a.GUID)).ToList();

So far this all works as expected.

Now I want to do a query where I find all rows in another DB table that have GUIDs from my list2

var list3 = db.MyTable.Where(a => list2.Contains(a.GUID)).ToList();

The data types are all the same in the three tables so I know those match. But I get the best overloaded match has some invalid arguments?

Upvotes: 0

Views: 214

Answers (1)

Markus Weninger
Markus Weninger

Reputation: 12668

You are missing the Where-clause in your third line:

var list3 = db.MyTable.Where(a => list2.Contains(a.GUID)).ToList();

EDIT: Okay, this was only a type and the question was edited, see new answer below.


Looking at your exception

System.Collections.Generic.List.Contains(Test1.Data.M‌​odels.Table1)' has some invalid arguments

We can see that list2 is of type List<Test1.Data.Models.Table1>, yet you try to run list2.Contains(long). You have to change

var list2 = db.Table2.Where(a => list1.Contains(a.GUID)).ToList();

to

var list2 = db.Table2.Where(a => list1.Contains(a.GUID)).Select(a => a.GUID).ToList();

Then list2 should be of type List.

I am personally not a big of var because you cannot extract the exact type of a variable from source code. If you change your vars to "real" data types you may see your problem far easier.

Upvotes: 1

Related Questions