tags:

views:

1974

answers:

3

Hey all,

EDIT: For the inner queries, there could be more than one match per inner query. It grabs a bunch of tags with the same game_ID. Thats why .First or .Max won't work.

Need some help, I have a query in LINQ that looks like this:

from yy in Tags_Lookups
where yy.Tag_ID == (from xx in Tags_Lookups
where xx.Game_ID == new Guid("4962d645-711c-4db8-a7ce-ae9b36dd730c")
select xx.Tag_ID)
select yy

I am getting an error that looks like this:

Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable'

I have also tried this with no luck:

from yy in Tags_Lookups
where yy.Tag_ID.Contains(from xx in Tags_Lookups
where xx.Game_ID == new Guid("4962d645-711c-4db8-a7ce-ae9b36dd730c")
select xx.Tag_ID)
select yy

With this Error:

'int' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'int' could be found

Can anyone help me write a good query that will work?

+1  A: 

Since your inner query can return multiple matches, you just need to convert the inner query to a list and reverse the sense of the contains clause, I think.

from yy in Tags_Lookups
where (from xx in Tags_Lookups
       where xx.Game_ID == new Guid("4962d645-711c-4db8-a7ce-ae9b36dd730c")
       select xx.Tag_ID).ToList()
                        .Contains( yy.Tag_ID )
select yy

EDIT Changed query based on new information.

tvanfosson
Look at the EDIT.
Scott
To bad I can't confirm both of you as the answer.
Scott
It looks like this one is faster even though it has more lines of LINQ and T-SQL syntax. by a matter of a few miliseconds....
Scott
A: 

Instead of Max() it should probably be SingleOrDefault(), since you should never get more than one record with a GUID lookup. Both will get you there though. The bottom line is that the inner query is a collection, and you need to tell it how to select one row from that collection.

gfrizzle
+1  A: 

Just saw the edit - I think this is what you want (check my syntax, though):

from yy in Tags_Lookups
join xx in Tags_Lookups on yy.Tag_ID Equals xx.Tag_ID
where xx.Game_ID == new Guid("4962d645-711c-4db8-a7ce-ae9b36dd730c")
select yy

That will get you a Tags_Lookups value for every matching Game_ID. You may want to wrap the whole thing in parenthesis and add ".Distinct" as well in case you might get multiple hits.

gfrizzle
To bad I can't confirm both of you as the answer.
Scott