Linq 尝试排除与另一个对象联接的记录时出错
在我下面的代码中,是否有任何方法可以使用对象“WasteRecordsIncluded”中的结果与searchResults结合,基本上排除了我不想要的WasteId 如果调试到最后一行,则会出现错误: base{System.SystemException}={“查询包含对在不同数据上下文中定义的项的引用。”} 或者,如果加入是不可能的,那么我可以将巴札都从真变假,从假变真,并进行某种“不在”的比较 有人帮帮忙吗?亲切问候:Linq 尝试排除与另一个对象联接的记录时出错,linq,Linq,在我下面的代码中,是否有任何方法可以使用对象“WasteRecordsIncluded”中的结果与searchResults结合,基本上排除了我不想要的WasteId 如果调试到最后一行,则会出现错误: base{System.SystemException}={“查询包含对在不同数据上下文中定义的项的引用。”} 或者,如果加入是不可能的,那么我可以将巴札都从真变假,从假变真,并进行某种“不在”的比较 有人帮帮忙吗?亲切问候: var allWaste = _securityReposi
var allWaste = _securityRepository.FindAllWaste(userId, SystemType.W);
var allWasteIndicatorItems = _securityRepository.FindAllWasteIndicatorItems();
// First get all WASTE RECORDS
var searchResults = (from s in allWaste
join x in allWasteIndicatorItems on s.WasteId equals x.WasteId
where (s.Description.Contains(searchText)
&& s.Site.SiteDescription.EndsWith(searchTextSite)
&& (s.CollectedDate >= startDate && s.CollectedDate <= endDate))
&& x.EWC.EndsWith(searchTextEWC)
select s).Distinct();
var results = searchResults;
if (hazardous != "-1")
{
// User has requested to filter on Hazardous or Non Hazardous only rather than Show All
var WasteRecordsExcluded = (from we in _db.WasteIndicatorItems
.Join(_db.WasteIndicators, wii => wii.WasteIndicatorId, wi => wi.WasteIndicatorId, (wii, wi) => new { wasteid = wii.WasteId, wasteindicatorid = wii.WasteIndicatorId, hazardtypeid = wi.HazardTypeId })
.Join(_db.HazardTypes, w => w.hazardtypeid, h => h.HazardTypeId, (w, h) => new { wasteid = w.wasteid, hazardous = h.Hazardous })
.GroupBy(g => new { g.wasteid, g.hazardous })
.Where(g => g.Key.hazardous == bHazardous && g.Count() >= 1)
select we);
// Now join the 2 object to eliminate all the keys that do not apply
results = results.Where(n => WasteRecordsExcluded.All(t2 => n.WasteId == t2.Key.wasteid));
}
return results;
var allWaste=\u securityRepository.FindAllWaste(userId,SystemType.W);
var allwasteindicationItems=_securityRepository.findallwasteindicationItems();
//首先获取所有废物记录
var searchResults=(来自allWaste中的s)
在s.WasteId等于x.WasteId的所有wasteIndicationItems中加入x
其中(s.Description.Contains)(搜索文本)
&&s.Site.sitesdescription.EndsWith(searchTextSite)
&&(s.CollectedDate>=开始日期和s.CollectedDate wii.WasteIndicationId,wi=>wi.WasteIndicationId,(wii,wi)=>new{wasteid=wii.wasteid,WasteIndicationId=wii.WasteIndicationId,hazardtypeid=wi.hazardtypeid})
.Join(_db.HazardTypes,w=>w.hazardtypeid,h=>h.hazardtypeid,(w,h)=>new{wasteid=w.wasteid,hazardous=h.hazardous})
.GroupBy(g=>new{g.wasteid,g.hazardous})
其中(g=>g.Key.hazardous==bHazardous&&g.Count()>=1)
选择我们);
//现在连接2对象以消除所有不适用的关键点
results=results.Where(n=>wasteRecordsIncluded.All(t2=>n.WasteId==t2.Key.WasteId));
}
返回结果;
可能是这样的:
.....
var results = searchResults.ToList();
.....
.....
.Where(g => g.Key.hazardous == bHazardous && g.Count() >= 1)
select we).ToList();
.....
您在哪里创建数据上下文?