如何从 entity framework 中的两个关联表中获取数据

how to fetch data from two associated tables in entity framework

我有两个 table 名为 Places 和 experiences 的一对多关系。我想从这些 table 中获取以下格式的数据-

[
 {
"PlaceId": 1,
"PlaceName": "Agra",
"Experience": [
  {
    "ExperienceId": 1,
    "ExperienceName": "TajMahel"
  },
  {
    "ExperienceId": 2,
    "ExperienceName": "AgraFort"
  }
   ]
  }
]

所以我正在编写这样的查询但无法正常工作。我不知道如何编写嵌套查询。

var exp=from log in db.Places where(log.IsActive==true)
select new
{
    log.Id,
    from log1 in log.Experiences where(log1.LanguageId==1) select new {log1.Id,log1.Title}
};

你可以试试这样的

     var exp = from log in db.Places
                      where log.IsActive==true
                      select new
                          {
                              logId= log.Id,,
                              experiences = from exp in log.Experiences 
                                         where(log1.LanguageId==1)
                                         select new
                                             {
                                                 id=log1.Id,
                                                 title=log1.Title
                                             }
                          };

Anonymous Type Projection

Linq Projection