我需要一个 API 端点,它将从单个 json 文件中的这四个表中获取记录。我正在使用 .NET Core 8 Web Api。在 QueryAsync 内部,我尝试了不同的组合,但它没有产生正确的结果。
目前,当我运行时,我可以重复图库和文本块记录,引擎运行良好。例如,这艘船在 Gallery 表中有 37 个图像,在 Textblock 表中有 8 个“BoatId”条目:“317557”。
但是两者都显示37x8 = 296 条记录,如图所示。我被困在这里,请帮忙。我的感觉是我无法使记录清晰,这导致了这个问题。请指导我。
public class Boat
{
public string BoatId { get; set; }
public string CreatedTimestamp { get; set; }
public string UpdatedTimestamp { get; set; }
public string Type { get; set; }
public string Status { get; set; }
public string ListingDate { get; set; }
public string Model { get; set; }
public string VesselName { get; set; }
public int? PriceUSD { get; set; }
public int LOAFeet { get; set; }
public string Manufacturer { get; set; }
public string Category { get; set; }
public List<Gallery> Gallery { get; set; }
public List<Textblock> Textblocks { get; set; }
public List<Engine> Engines { get; set; }
}
public class Gallery
{
public int GalleryID { get; set; }
public string GalleryBoatId { get; set; }
public string Image { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Featured { get; set; }
public int Sort { get; set; }
public string DateTimeStamp { get; set; }
}
public class Textblock
{
public string TextBlockId { get; set; }
public string TextBlockBoatId { get; set; }
public string Language { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Sort { get; set; }
}
public class Engine
{
public int EngineId { get; set; }
public string EngineBoatId { get; set; }
public string EngineMake { get; set; }
public string EngineModel { get; set; }
public string EngineYear { get; set; }
public string EngineType { get; set; }
public string EngineLocation { get; set; }
public int Sort { get; set; }
}
public async Task<List<Boat>> GetAllVesselListings()
{
var query = @"SELECT
boats.BoatId As ListingBoatID, boats.*
,gallery.GalleryBoatId As GalleryBoatID, gallery.*
,Textblock.TextBlockBoatId As TextblockBoatID, textblock.*
,engine.EngineBoatId As EngineBoatID, engine.*
FROM View_IybaBoats boats
INNER JOIN View_IybaImageGallery gallery ON boats.BoatId = gallery.GalleryBoatId
LEFT OUTER JOIN View_IybaTextBlock textblock ON boats.BoatId = textblock.TextBlockBoatId
LEFT OUTER JOIN View_IybaEngine engine ON boats.BoatId = engine.EngineBoatId
order by boats.BoatId, gallery.Sort
";
var boatDict = new Dictionary<int, Boat>();
var boats = await _db.QueryAsync<Boat, Gallery, Textblock, Engine, Boat>
(query, (boat, gallery, textblock, engine) =>
{
if (!boatDict.TryGetValue(Convert.ToInt32(boat.BoatId), out var currentBoat))
{
currentBoat = boat;
currentBoat.Gallery = new List<Gallery>();
currentBoat.Textblocks = new List<Textblock>();
currentBoat.Engines = new List<Engine>();
currentBoat.Engines.Add(engine);
boatDict.Add(Convert.ToInt32(currentBoat.BoatId), currentBoat);
}
currentBoat.Gallery.Add(gallery);
currentBoat.Textblocks.Add(textblock);
return currentBoat;
},
splitOn: "ListingBoatID,GalleryID,TextBlockId,EngineId");
return boats.Distinct().ToList();
}
我尝试将它们放在 if(!boatDict.TryGetValue... 语句中,然后每个值只有一个值。
currentBoat.Gallery.Add(画廊); currentBoat.Textblocks.Add(textblock);
我需要使图库和文本块仅保留不同的值,但它没有发生。请推荐。
我已经浏览了这个网站上的很多帖子,代码看起来没有任何问题,但结果没有出来。
通过
boats
得到.QueryAsync()
后,可以对投影中的记录进行分组/区分。
var boats = await _db.QueryAsync<Boat, Gallery, Textblock, Engine, Boat>QueryAsync(/* ... */);
boats = boats.Select(x => new Boat
{
Gallery = x.DistinctBy(y => y.GalleryID).ToList(),
Textblocks = x.DistinctBy(y => y.TextBlockId).ToList(),
Engines = x.Engines.ToList()
})
.ToList();
return boats.Distinct().ToList();