Pass table as parameter in LINQ

  • Thread starter Thread starter Luna
  • Start date Start date
L

Luna

I have a Tags table that is shared by Photos and Videos tables

Photos
-----
Id
Name
.....

Videos
-----
Id
Name
.....

Tags
------
Id
ParentId
ParentType

I would like to write a function that returns the parent object(photo, or
video) based on tagId

the parameter tableData could be
from p in data.Photos or
from v in data.Videos

public void GetParentData(int tagId, IQueryable tableData)
{
var temp = from d in tableData
where (from t in data.Tags select
t.ParentId).Contains(d.id)
select d;
}

Im not sure how to write this in LINQ if it's even possible.
With T-SQL I can just replace the tablename string with the string parameter
passed in.


thx
Luna
 
Luna said:
I have a Tags table that is shared by Photos and Videos tables

Photos
-----
Id
Name
....

Videos
-----
Id
Name
....

Tags

Never, ever do this. This is really really bad database design. You should
create a VideoID column and PhotoID column. That way you can still have
foreign keys. With your design you can't create foreign keys so can't have
any integrity on the database. As an added bonus queries become easier to
write and you don't find yourself jumping through the hoops you will have to
with the "ParentType" kind of solution. The very fact that you're asking the
question shows that it is already giving you troubles.

If you're stuck with this design you might need to use a union or an if
statement and write 2 queries.

Michael
 
Back
Top