Return anonymous type results?

后端 未结 16 1126
梦毁少年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:56

    Try this to get dynamic data. You can convert code for List<>

    public object GetDogsWithBreedNames()
    {
        var db = new DogDataContext(ConnectString);
        var result = from d in db.Dogs
                     join b in db.Breeds on d.BreedId equals b.BreedId
                     select new
                            {
                                Name = d.Name,
                                BreedName = b.BreedName
                            };
        return result.FirstOrDefault();
    }
    
    dynamic dogInfo=GetDogsWithBreedNames();
    var name = dogInfo.GetType().GetProperty("Name").GetValue(dogInfo, null);
    var breedName = dogInfo.GetType().GetProperty("BreedName").GetValue(dogInfo, null);
    

提交回复
热议问题