0

I am trying to retrieve a data from database and showing it on my html table but I am getting errors.

The foreach statement look like this:

foreach(PFEvent events in con.PFEvents)
                {                   
                    DataRow row = dt.NewRow();
                    row["Sport"] = events.Sport;
                    row["Description"] = events.Description;
                    row["Date"] = events.Date.ToString();
                    row["Time"] = events.Time.ToString();
                    row["Status"] = events.Status;                  
                    row["Stadium Name"] = from PFStadium in con.PFStadiums
                                          join PFEvent in con.PFEvents on PFStadium.Stadium_ID equals events.Stadium_ID
                                          select PFStadium.StadiumName;

                    dt.Rows.Add(row);
                }                

This is the problem code while everything else returns what it is meant to return:

row["Stadium Name"] = from PFStadium in con.PFStadiums
                                          join PFEvent in con.PFEvents on PFStadium.Stadium_ID equals events.Stadium_ID
                                          select PFStadium.StadiumName;

This query returns the value like this:

SELECT [t0].[StadiumName] FROM [dbo].[PFStadium] AS [t0] INNER JOIN [dbo].[PFEvent] AS [t1] ON ([t0].[Stadium_ID]) = @p0

I wanted to return the Stadium name but it gives me a query as a return. How do i solve this?

Sushan Limbu
  • 43
  • 1
  • 8
  • what kind of objects are PFEvents and PFStadium? – McNets Nov 19 '16 at 23:08
  • the `row["Stadium Name"] = from PFStadium ...` query returns an `Enumerable` (even if it returns a single element). Call `.First()` or `ToArray()[0]` to get the first one – rbm Nov 19 '16 at 23:08
  • They are table classes called using the creation of data through the data context gateway. – Sushan Limbu Nov 19 '16 at 23:16

2 Answers2

1

You can use:

(from PFStadium in con.PFStadiums join PFEvent in con.PFEvents on PFStadium.Stadium_ID equals events.Stadium_ID
                                      select PFStadium.StadiumName).FirstOrDefault();
Klinger
  • 4,900
  • 1
  • 30
  • 35
  • This works now thank you but how did it work? Can you please explain if you don't mind? – Sushan Limbu Nov 19 '16 at 23:17
  • What you had before returns a IQueryable (or IEnumerable depending on what con.PFStadiums). FirstOrDefault basically does a Top 1 on your query. The following SO post has some good info: http://stackoverflow.com/questions/1024559/when-to-use-first-and-when-to-use-firstordefault-with-linq. And also this link: http://www.technicaloverload.com/linq-single-vs-singleordefault-vs-first-vs-firstordefault/ – Klinger Nov 19 '16 at 23:24
0

It's all right in your method, but you don't have data, you have only IQueryable commands how to get data.

You need to get your data, so:

row["Stadium Name"] = (from PFStadium in con.PFStadiums
    join PFEvent in con.PFEvents on PFStadium.Stadium_ID equals events.Stadium_ID
    select PFStadium.StadiumName).ToList();

ToList, ToArray, Sum, Count, etc all methods that return not IQueryable interface, will help you.

Yurii N.
  • 5,455
  • 12
  • 42
  • 66