Deepal
Deepal

Reputation: 1814

Debug Lambda expressions in C#

I am querying a SQL Server database from my ASP.net application in C#. Following is a part of my database schema.

enter image description here

I wrote following expression to get data from the database.

var query = db.DBMovies.Where(ent => ent.Id == Id).Join(db.DBCategories,
    r => r.Id,
    l => l.CategoryId,
    (r, l) => new
    {
       Id = r.Id,
       MovieName = r.Name,
       Year = r.Year,
       Category = r.Genre,
       PosterURL = r.PosterURL,
});

foreach (var movie in query)
{
    //something
}

In the part ent.Id == Id, the Id in the right refers to a parameter value. I referred this tutorial, but when I execute this, the execution does not get inside the foreach loop. I created a breakpoint inside the foreach loop and noticed that it didn't hit. Can somebody help me to find out the issue? And please suggest a way to quick watch the values returned by a lambda expression if possible.

Thank you.

Edit:

I changed the query as I have made a huge mistake first time, but I don't see any success even now.

var query = db.DBMovieToCategory.Where(ent => ent.CategoryId == Id).Join(db.DBMovies,
r => r.MovieId,
l => l.Id,
(r, l) => new
{
    Id = l.Id,
    MovieName = l.Name,
    Year = l.Year,
    Category = l.Genre,
    PosterURL = l.PosterURL,
});

Upvotes: 0

Views: 948

Answers (1)

Ann L.
Ann L.

Reputation: 13965

It looks like you're trying to join Movies to Categories by matching the Movie id to the Category id. Since the IDs are assigned independently, you will only get records back if by chance you have a Movie and a Category with the same ID.

Getting no records back would be why you never enter the foreach loop.

I see no relationships defined in your model, so DBMovies, DBCategories, and DBMoviesToCategories don't "know" they're related to each other.

Upvotes: 1

Related Questions