views:

63

answers:

2

hi all. I have Three Tables in SqlServer (equivalent to image in link below in SQL Server)

http://www.mojoimage.com/free-image-hosting-view-06.php?id=2Untitled-2.gif

and when i use LINQ to Sql i get three classes in dbml file:

class Item
{
Guid ItemId;
int ItemSize;
}

class Video
{
Guid RecordId;
Guid ItemId;
string Resolution;
}

class Audio
{
Guid RecordId;
Guid ItemId;
int Duration;
}

then i create this three interfaces:

Interface IItem
{
Guid ItemId;
int ItemSize;
}

Interface IVideo
{
string Resolution;
}

Interface IAudio
{
int Duration;
}

and a class named VideoItem that implements both "Video" interface and "Item" interface:

class VideoItem:IItem,IVideo
{
public Guid ItemId;
public int ItemSize;
public string Resolution;
}

and a class named AudioItem that implements both "Audio" interface and "Item" interface.

class AudioItem :IItem,IVideo
{
public Guid ItemId;
public int ItemSize;
public int Duration;
}

now i want to use LINQ to query VideoItem instances from my tables (those are in both Item table and Video table).

do i need to create custom LINQ provider?

A: 
from ai in Audio
where ai.XXXX == whatever
select new AudioItem
    {
        ItemId = ai.ItemId,
        ItemSize = ai.Item.ItemSize,
        Duration = ai.DUration
    }

This assume that you have a foreign-key releationship set up between Item & Audio based on ItemId. If not, you'll have to go with:

from ai in Audio
join i in Item on ai.ItemId equal i.ItemiD
where ai.XXXX == whatever
select new AudioItem
    {
        ItemId = ai.ItemId,
        ItemSize = i.ItemSize,
        Duration = ai.DUration
    }

UPDATE: You can create a List of AUdioItems like this:

var listOfAudioItems =
(from ai in Audio
where ai.XXXX == whatever
select new AudioItem
    {
        ItemId = ai.ItemId,
        ItemSize = ai.Item.ItemSize,
        Duration = ai.DUration
    }).ToList();

Once you have that, you can use it in a LINQ state as a single table:

 from ai in listOfAudioItems
 where ai.Duration > 5 && ai.ItemSize == 10
 select ai;
James Curran
thanks but i want to Use VideoItem and AudioItem as this:from Vi in Context.VideoItems select ... not from separated tables
backdoor
But you *have* separate tables. You have to deal with them separately first. These queries will *create* a List<AudioItem> which you can then deal with as a single table.
James Curran
with custom LINQ provider i can't do what i sayd?thanks
backdoor
First of all, how is what you want to do greatly different than my first example? I imagine what you want could be done with a custom provider, but it would be a massive undertaking to merely save having to create a foreign-key releationship.
James Curran
becuase of this is a part or my DataLayer i want to encapsulate all those things and get user just one choise...
backdoor
is there a simple way to create the custom LINQ provider for this type of problem i have?
backdoor
A: 

Hey,

The solution provided by @James will be a lot easier than writing a custom LINQ provider. You can select a join between the two entities by using a LINQ query joining the two tables and selecting the results as an AudioItem or VideoItem respectively.

Brian
becuase of this is a part or my DataLayer i want to encapsulate all those things and get user just one choise...
backdoor
is there a simple way to create the custom LINQ provider for this type of problem i have?
backdoor
LINQ providers would be too much work, and probably wouldn't gain the benefit you are looking for very easily... ADO.NET Entity Framework gives you more choices to develop DAL components generically...
Brian