实体查询返回所有内容的重复记录

时间:2017-03-16 17:46:15

标签: c# sql-server entity-framework entity-framework-6

我的查询存在问题,我终于可以解决,但现在即使使用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;
            }

1 个答案:

答案 0 :(得分:0)

如果有人好奇,我可以通过我知道将在数据集中的两个实体键对项目进行分组来解决问题。