We help IT Professionals succeed at work.

linq NOT IN using datatables / AsEnumerable

swgdesign
swgdesign asked
on
I am not using a datacontext and so I am using datatables.AsEnumerable() to use LINQ as a subquery tool.

I have 2 datatables defined as follows;

 
Dim dtObjectTypes As DataTable = IMCSelect.getObjectTypes(-1, Application("logUsers"), 1).Tables(0)

Dim dtPermissions As DataTable = IMCSelect.getPermissions(iUserID, -1, Application("logUsers")).Tables(0)

Open in new window


I have started to write a LINQ query to get any items from dtObjectTypes that DO NOT appear in dtPermissions, but VS2010 is telling me the following error;

"o hides a variable in an enclosing block...etc"

 
Dim query = From o In dtObjectTypes.AsEnumerable() Where Not o.Field(Of Integer)("ID") = (From p In dtPermissions.AsEnumerable() Select p.Field(Of Integer)("TypeID") Select o)

Open in new window


Comment
Watch Question

SILVER EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Within the function where you defined this query, you have also declared another variable named "o".

Author

Commented:
I get the error message, my questin was, what needs changing to get the query working?
SILVER EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Change "o" in your query to some other variable name.
GlobaLevelProgrammer

Commented:
as Kaufmed mentioned you double declared the same variable

Author

Commented:
How can I change 'o' when 'o' is an item of ObejctTypes that I need to return?

I've tried changing the last o and it errors.
SILVER EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Dim query = From MySuperAwesomeNewVariableThatWontHideAnythingBecauseItHasANewScope In dtObjectTypes.AsEnumerable() Where Not MySuperAwesomeNewVariableThatWontHideAnythingBecauseItHasANewScope.Field(Of Integer)("ID") = (From p In dtPermissions.AsEnumerable() Select p.Field(Of Integer)("TypeID") Select MySuperAwesomeNewVariableThatWontHideAnythingBecauseItHasANewScope)

Open in new window

Author

Commented:
Dim query = From ot In dtObjectTypes.AsEnumerable() Where Not ot.Field(Of Integer)("ID") = (From p In dtPermissions.AsEnumerable() Select p.Field(Of Integer)("TypeID") Select ot)

Open in new window


Causes the same issue as you are using ot in 3 places including the sub query!
Any ideas?
Found the solution guys...

 
Dim query = From o In dtObjectTypes.AsEnumerable() Where Not (From p In dtPermissions.AsEnumerable() Select p.Field(Of Integer)("TypeID")).Contains(o.Field(Of Integer)("ID")) Select o

Open in new window

Author

Commented:
Rewrote the query to use .Contains

Explore More ContentExplore courses, solutions, and other research materials related to this topic.