Return anonymous type results?

后端 未结 16 1137
梦毁少年i
梦毁少年i 2020-11-22 03:01

Using the simple example below, what is the best way to return results from multiple tables using Linq to SQL?

Say I have two tables:

Dogs:   Name, A         


        
16条回答
  •  情话喂你
    2020-11-22 03:50

    If you have a relationship setup in your database with a foriegn key restraint on BreedId don't you get that already?

    So I can now call:

    internal Album GetAlbum(int albumId)
    {
        return Albums.SingleOrDefault(a => a.AlbumID == albumId);
    }
    

    And in the code that calls that:

    var album = GetAlbum(1);
    
    foreach (Photo photo in album.Photos)
    {
        [...]
    }
    

    So in your instance you'd be calling something like dog.Breed.BreedName - as I said, this relies on your database being set up with these relationships.

    As others have mentioned, the DataLoadOptions will help reduce the database calls if that's an issue.

提交回复
热议问题