Pass table as parameter in LINQ

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
 
M

Michael C

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top