James Wilson
James Wilson

Reputation: 5150

ViewBag select list LINQ table join

Is it possible to join tables in a SelectList?

Here is what I have:

ViewBag.Clients = new SelectList(db.IPACS_Clients, "clientID", "name", 0);

I need to join the db.IPACS_Clients_Network table, I still need "clientID" but for "name" I need to be able to select IPACS_Clients.name + " - " + IPACS_Clients_Network.name

Is this possible with a Viewbag?

Upvotes: 1

Views: 1398

Answers (1)

James Wilson
James Wilson

Reputation: 5150

While this way is not recommended, I was able to find the answer.

ViewBag.Clients = new SelectList(db.IPACS_Clients_Network
       .Join(
          db.IPACS_Clients,
          v => v.networkClientID,
          s => s.networkClientID,
          (v, s) =>
             new
             {
                 v = v,
                 s = s
             }
       )
       .Select(
          temp0 =>
             new
             {
                 v = temp0.v,
                 s = temp0.s
             }
       ).Select(m => new SelectListItem
       {
           Value = SqlFunctions.StringConvert((double)m.s.clientID).Trim(),
           Text = m.v.name + " - " + m.s.name
       }), "Value", "Text", 0);

Upvotes: 1

Related Questions