2012-12-13 168 views
0

請讓我知道我將如何使Linq查詢出來。Sql Linq查詢

SELECT 
[GroupBy1].[K1] AS [AccountName], 

[GroupBy1].[K2] AS [LeadBy], 

[GroupBy1].[K3] AS [Status], 

[GroupBy1].[K3] AS [Industry], 

[GroupBy1].[A1] AS [C2] 

FROM (SELECT 
    [Extent1].[AccountName] AS [K1], 

    [Extent2].[LeadBY] AS [K2], 

    [Extent2].[Status] AS [K3], 

    [Extent2].[Industry] AS [K4], 

    COUNT(1) AS [A1] 

    FROM [dbo].[Opportunities] AS [Extent1] 

    join [dbo].[Leads] AS [Extent2] 

    on [Extent1].[AccountName] IS NOT NULL 

    AND [Extent1].[AccountName]=[Extent2].[AccountName] 

    GROUP BY [Extent1].[AccountName],[Extent2].[Leadby],[Extent2].[Status],[Extent2]. 
[Industry] 
) AS [GroupBy1] 

回答

0

嘗試使用這個程序: http://www.linqpad.net/它有一個基本的和先進的操作實例。您也可以下載其他教程。

+0

但是這個時候你能幫我一下,因爲我急需有兩個表的潛在顧客和Oppurtunity,我想要得到由Lead創建的Oppurtunities數量 – user1847109

0

它deppends你的映射,如果你還沒有一個外鍵領先進入帳戶名欄,你應該試試這個:

from i in Context.Opportunities 
join l in Context.Leads on l.AccountName equals i.AccountName 
group i by new { i.AccountName, l.LeadBY, l.Status, l.Industry } into g 
select new 
{ 
    g.Key.AccountName, 
    g.Key.LeadBY, 
    g.Key.Status, 
    g.Key.Industry, 
    Count = g.Count(); 
} 

,如果你有一個外鍵領先進入帳戶名欄,你應該試試這個:

from i in Context.Opportunities 
group i by new { i.AccountName, l.Lead.LeadBY, l.Lead.Status, l.Lead.Industry } into g 
select new 
{ 
    g.Key.AccountName, 
    g.Key.LeadBY, 
    g.Key.Status, 
    g.Key.Industry, 
    Count = g.Count(); 
}