我有3个需要链接在一起的对象

父级:TblClients

这将具有TblBusinessLeads类型的多个子代,两者之间的键是ClientID

Lead类型将有多个TblFeeBreakouts类型的子项,两者之间的键是LeadID

我已经编写了以下LINQ来获取数据返回,但是它没有返回(内存不足异常)

from t0 in TblClients
join t1 in TblBusinessLeads on t0.ClientID equals t1.ClientID into t1_join
from t1 in t1_join.DefaultIfEmpty()
join t3 in TblFeeBreakouts on t1.LeadID equals t3.LeadID into t3_join
from t3 in t3_join.DefaultIfEmpty()
orderby
t0.ClientID,
t1.LeadID
select new {
            client_data = t0,
            business_lead_data = t1_join,
            fee_breakout_data = t3_join
}


我不确定您是否可以做到这一点,但是这个想法似乎很普遍。任何帮助将不胜感激。谢谢

编辑:
哇很多评论。这是我的答案
我正在尝试在LinqPad中运行查询,那就是内存不足正在发生的地方
如果我看看生成的SQL,它给了我

SELECT [t0].[ClientID], [t0].[ClientName], [t0].[ClientDesc], [t0].[EditedBy], [t0].[EditedDate], [t0].[CreatedBy], [t0].[CreatedDate], [t3].[LeadID], [t3].[InitiativeName], [t3].[Description], [t3].[NewBusNeeds], [t3].[CreativeNeeds], [t3].[IdeationNeeds], [t3].[Comments], [t3].[LossReasons], [t3].[OriginDate], [t3].[DateReceivedAssignment], [t3].[DueDate], [t3].[TimelineNotes], [t3].[PendingCode], [t3].[EstStartDate], [t3].[EstEndDate], [t3].[ExeStartDate], [t3].[ExeEndDate], [t3].[Probable80Total], [t3].[Possible50Total], [t3].[Emerging25Total], [t3].[NoBudget0Total], [t3].[TotalBudget], [t3].[FinancialNotes], [t3].[DollarsRecordFor], [t3].[BizDevContactUserID], [t3].[BizDevContact2UserID], [t3].[SVPContactUserID], [t3].[ClientMgmtContactUserID], [t3].[CMAdditionalContactUserID], [t3].[AdditionalContactUserID], [t3].[CreatorUserID], [t3].[OfficeID], [t3].[ClientID] AS [ClientID2], [t3].[LeadTypeID], [t3].[ActionNeeded], [t3].[ActionDate], [t3].[NewBusDeliveryDate], [t3].[NewBusDesc], [t3].[CreativeDeliveryDate], [t3].[CreativeDesc], [t3].[IdeationDeliveryDate], [t3].[IdeationDesc], [t3].[AltMediaDeliveryDate], [t3].[AltMediaDesc], [t3].[MobileOpsDeliveryDate], [t3].[MobileOpsDesc], [t3].[EventsDeliveryDate], [t3].[EventsDesc], [t3].[Routing], [t3].[RoutingDate], [t3].[Deleted], [t3].[LeadSourceID], [t3].[NatureofLeadID], [t3].[NatureofLeadNotes], [t3].[EditedBy] AS [EditedBy2], [t3].[EditedDate] AS [EditedDate2], [t3].[CreatedBy] AS [CreatedBy2], [t3].[CreatedDate] AS [CreatedDate2], [t3].[ClientContactName], [t3].[ClientContactTitle], [t3].[ReportingYear], (
SELECT COUNT(*)
FROM [tblBusinessLead] AS [t4]
WHERE [t0].[ClientID] = [t4].[ClientID]
) AS [value], [t1].[LeadID] AS [LeadID2]
 FROM [tblClient] AS [t0]
 LEFT OUTER JOIN [tblBusinessLead] AS [t1] ON [t0].[ClientID] = [t1].[ClientID]
LEFT OUTER JOIN [tblFeeBreakout] AS [t2] ON [t1].[LeadID] = [t2].[LeadID]
 LEFT OUTER JOIN [tblBusinessLead] AS [t3] ON [t0].[ClientID] = [t3].[ClientID]
 ORDER BY [t0].[ClientID], [t1].[LeadID], [t2].[LeadID], [t2].[FeeTypeID], [t3].    [LeadID]


这样返回120万行

模型中没有映射,因为数据库没有关系(它们是推断的,没有外键或类似的东西)

我使用t1_join和t3_join的原因是因为如果使用t1或t3,则将获得单个实体,而不是对象的IEnumerable,因此无法对其进行循环。

如果您还有其他问题,请询问。

最佳答案

首先,一个客户可以为120万行使用什么功能?对此没有真正的好用例,因此第一步应该弄清楚如何适当地过滤结果。

其次,我相信您的查询返回OutOfMemoryException的原因是因为LinQPad正在执行ToList()或类似的操作,因此我可以在底部窗格中显示查询的结果。 ToList()正在将所有120万行加载到内存中。如果您在常规.Net应用程序中运行查询,则以下内容将返回一个IQueryable<>,它将不会加载到内存中。

var query =
from t0 in TblClients
join t1 in TblBusinessLeads on t0.ClientID equals t1.ClientID into t1_join
from t1 in t1_join.DefaultIfEmpty()
join t3 in TblFeeBreakouts on t1.LeadID equals t3.LeadID into t3_join
from t3 in t3_join.DefaultIfEmpty()
orderby
t0.ClientID,
t1.LeadID
select new {
            client_data = t0,
            business_lead_data = t1_join,
            fee_breakout_data = t3_join
};

关于c# - 2级连接LINQ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11416521/

10-10 10:24