0

I have a database with three tables as below

Tbl 1

Product - ID, Name, Description, MediaType

Tbl 2

Media - ID, MediaName, MediaDescription

Tbl3

ProductAndMedia - ID, MediaID, ProductID

In tbl3 MediaID and ProductID are Foreign Keys.

I then have a Linq query

Dim query = From MyDataContext.ProductAndMedia.Where(Function(i) i.ProductID = PiD).ToList()

It is then made as a DataSource for a radioButton

rbt.DataSource = query
rbt.DataTextField = ????
rbt.DataValueField = "ID"
rbt.DataBind()

What i am trying to achieve is to have the MediaName to be the TextField for the radionButton list. If i use MediaID, ProductID as the textfield then it shows the ID for that particular product/media but i am after the Name in this instance.

Is there anyway to get the name in this way or would i need to make another call to the database to get the name by ID?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Computer
  • 2,149
  • 7
  • 34
  • 71
  • rbt.DataTextField = the name of the field in the table you want to display. – OneFineDay May 30 '14 at 20:41
  • Using MediaName produces an error that the field cannot be found – Computer May 30 '14 at 20:44
  • That is because `MediaName` does not belong to `MyDataContext.ProductAndMedia`, see what table your calling? – OneFineDay May 30 '14 at 20:49
  • Thats correct however when i use Linq to SQL i usually can do something like ....ProductAndMediaSource.FirstOrDefault().Media.MediaName so i was wondering if there is an alternative to the way im using it above? – Computer May 30 '14 at 20:51
  • I don't think you can bind a List to the `DataSource` just one object - it's not a collection. – OneFineDay May 30 '14 at 21:04

1 Answers1

0

You could select the fields you are interested in as a new anonymous type (or create a class, or use KeyValuePair, etc.).

Dim query = From MyDataContext.ProductAndMedia _
    .Where(Function(i) i.ProductID = PiD) _
    .Select(Function(i) New With { .ID = i.ID, .MediaName = i.Media.MediaName }) _
    .ToList()
rbt.DataSource = query
rbt.DataTextField = "MediaName"
rbt.DataValueField = "ID"
rbt.DataBind()

Update

Looks like this is probably the same as this question.

Community
  • 1
  • 1
Mark
  • 8,140
  • 1
  • 14
  • 29