Sushan Limbu Sushan Limbu - 17 days ago 7
C# Question

C# How to do Linq Query

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?

Answer

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();