0
我的查詢出現了問題,我終於可以解決問題了,但是即使使用Distinct(),現在也會形成重複日期。我知道這些連接是混亂的,不幸的是這是我必須做的,因爲我正在使用的表格之間沒有任何關係。實體查詢返回所有內容的重複記錄
try
{
//work on query further , need to get client ID correctly
if (vehicleses.Count == 0)
return null;
string siteId = QueryExportSiteWithId(exportSiteId).SiteId;
// db.Database.Log = Console.Write;
var joinedInventorySettings = await (from id in db.Inventory_Descriptions
join iv in db.Inventory_Vehicles
on new {client = id.ClientID, id = id.InventoryID} equals new {client = iv.ClientID, id = iv.ID}
into descGroup
from m in descGroup.DefaultIfEmpty()
join se in db.Settings_Exports
on m.ClientID equals se.ClientID into settingGroup
from sg in settingGroup.DefaultIfEmpty()
join sl in db.Settings_Lots
on new {client = m.ClientID, id = m.LotID} equals new {client = sl.ClientID, id = sl.ID} into
lotsGroup
from lg in lotsGroup.DefaultIfEmpty()
join ses in db.Settings_ExportSites on new {client = m.ClientID, lotId = m.LotID, site = siteId}
equals new {client = ses.ClientID, lotId = ses.LotID, site = ses.Site} into exportGroup
from eg in exportGroup.DefaultIfEmpty()
join ifs in db.Inventory_Features
on new {client = m.ClientID, id = m.ID} equals new {client = ifs.ClientID, id = ifs.InventoryID}
into invFeatGroup
from ifg in invFeatGroup.DefaultIfEmpty()
join ip in db.Inventory_Photos
on m.ID equals ip.InventoryID into photo
from photos in photo.DefaultIfEmpty()
where m.Archived != "1"
&& m.Holding != "1"
&& m.Status == "A"
&& clientIdList.Contains(m.ClientID)
select new JoinedInventorySettings()
{
InventoryVehicles = m,
InventoryDescriptions = id,
SettingsExports = sg,
//InventoryPhotos = ,
SettingsLots = lg,
InventoryFeatures = ifg,
SettingsExportSites = eg
}).Distinct().ToListAsync();
if (joinedInventorySettings != null)
{
returnList.AddRange(joinedInventorySettings);
return returnList;
}
return null;
}
您可以(也應該)在您的實體模型中定義關係,以便您可以擺脫這些連接。您的問題通常是由主鍵(如實體框架已知)導致的,它們實際上並不是唯一的*標識符。 –
@GertArnold如果我在edmx文件中定義關係,是否可以治癒我遇到的一些速度問題?看起來如果我這樣做,我應該分開庫存和設置之間的關聯,因爲它們不共享相同的主鍵,它們只共享一個。 – Cowmoogun
@gertarnold哎呀,我不是說速度,我的意思是重複。每行有大約2-4個重複項被檢索。 – Cowmoogun