2010-11-03 141 views
0

所以我試圖解決我的Silverlight報告應用程序中缺乏存儲過程支持,並且我的linq有點麻煩。實體框架/ linq查詢問題

我有一個存儲過程,看起來像這樣:

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
-- ============================================= 
-- Author: Some Dev Guy 
-- Create date: 11/02/10 
-- ============================================= 
Alter PROCEDURE spGetTopReferers 
@p_sitekey SmallInt, 
@p_startDate SmallDateTime, 
@p_endDate SmallDateTime 
AS 
BEGIN 

SET NOCOUNT ON; 

SELECT 
    TOP 10 
    SUM(DaySummaryReferrers.Visits) AS Visits, 
    SUM(DaySummaryReferrers.NewVisitors) AS 'New Visitors', 
    SUM(DaySummaryReferrers.Prospects) AS Prospects, 
    SUM(DaySummaryReferrers.Customers) AS Customers, 
    Referrers.Referrer 
    FROM 
    DaySummaryReferrers 
    LEFT OUTER JOIN 
    Referrers 
    ON 
    DaySummaryReferrers.ReferrerID = Referrers.ReferrerID 
    Where 
    DaySummaryReferrers.SiteKey = @p_sitekey 
    AND 
    DaySummaryReferrers.Dated 
    Between 
    @p_startDate 
    AND 
    @p_endDate 
    GROUP BY 
    Referrers.Referrer 
    ORDER BY 
    Visits DESC; 
END 
GO 

我已經創建瞭如下的DomainService類,這樣我可以使用實體框架查詢這一天。我想我的查詢的結果推到我的自定義數據結構監守我沒有說有,我需要爲我的報告中所有信息的實體(特別是訪問和引用)

namespace Reports.Web.Services 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.ComponentModel.DataAnnotations; 
    using System.Linq; 
    using System.ServiceModel.DomainServices.Hosting; 
    using System.ServiceModel.DomainServices.Server; 
    using System.Data; 
    using System.Data.Objects; 

    public class TopReferers 
    { 
     [Key] 
     [Editable(false)] 
     public int reffererID { get; set; } 
     public int? Visits { get; set; } 
     public int? Visitors { get; set; } 
     public int? Prospects { get; set; } 
     public int? Customer { get; set; } 
     public String Referrer { get; set; } 

    } 

    [EnableClientAccess()] 
    public class WebReportAggregateService : DomainService 
    { 
     WhosOnV5DevEntities ctx = new WhosOnV5DevEntities(); 

     public IQueryable<TopReferers> GetTopReferrers() 
     { 

      DateTime p_start = new DateTime(2010, 01, 01); 
      DateTime p_end = new DateTime(2010, 11, 01); 

      ObjectSet<DaySummaryReferrer> myReferrers = ctx.DaySummaryReferrers; 
      ObjectSet<Referrer> myReferrerNames = ctx.Referrers; 


      IQueryable<TopReferers> x = from referrer in myReferrers.Take(10) 
             join referrerName in myReferrerNames 
             on referrer.ReferrerID 
             equals referrerName.ReferrerID 
             where 
             referrer.SiteKey == 74 
             && 
             referrer.Dated >= p_start 
             && 
             referrer.Dated <= p_end 
             group referrer by referrerName.Referrer1 into g 
             select new TopReferers { Visits = g.Key.Visits, Customer = g.Key.Customers, Prospects = g.Key.Prospects, Visitors = g.Key.NewVisitors, Referrer = g.Key.Referrer, reffererID = g.Key.ReferrerID }; 


      return x; 
     } 

    } 
} 

這是我得到的錯誤:

select new TopReferers { Visits = g.Key.Visits, Customer = g.Key.Customers, Prospects = g.Key.Prospects, Visitors = g.Key.NewVisitors, Referrer = g.Key.Referrer, reffererID = g.Key.ReferrerID }; 

錯誤:

Error 2 'string' does not contain a definition for 'Customers' and no extension method 'Customers' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) C:\Users\User\documents\visual studio 2010\Projects\Reports\Reports.Web\Services\WebReportAggregateService.cs 53 108 Reports.Web 

我得到這個錯誤的訪問,自定義,前景,遊客,referrer和ReferrerID。

任何幫助,將不勝感激= d

+3

您可以通過實際包括在你的問題的錯誤開始...... – 2010-11-03 17:12:46

回答

0

Referrer.Referrer1是什麼類型的?從錯誤,這聽起來像你的組密鑰是string

我想你想要做的是通過組合鍵組:

group referrer by new { ID = referrerName.ReferrerID, Name = referrerName.Referrer1 } 

,然後選擇這樣的:

select new TopReferers { referrerID = g.Key.ID, 
         Visits = g.Sum(x => x.Visits), 
         Visitors = g.Sum(x => x.NewVisitors), 
         Prospects = g.Sum(x => x.Prospects), 
         Customer = g.Sum(x => x.Customers), 
         Referrer = g.Key.Name } 
+0

我的組密鑰是一個字符串,從您的句子的上下文我假設組密鑰需要是一個int? – BentOnCoding 2010-11-03 18:00:58

+0

@Robotsushi,no - 這會產生類似的錯誤,試圖在'int'上調用'Customers'方法。請看我編輯的答案 - 我不完全確定ID和姓名的來源,但我認爲這很接近。 – 2010-11-03 18:17:39

1

在你的類屬性你有Customer單數和在LINQ你有Customers複數。

public class TopReferers 
{ 
    [Key] 
    [Editable(false)] 
    public int reffererID { get; set; } 
    public int? Visits { get; set; } 
    public int? Visitors { get; set; } 
    public int? Prospects { get; set; } 
    public int? **Customer** { get; set; } 
    public String Referrer { get; set; } 

} 
+0

爲所有g.Key屬性生成此錯誤。 錯誤2'字符串'不包含'Customers'的定義並且沒有擴展方法'Customers'接受類型'string'的第一個參數可以找到(你是否缺少using指令或程序集引用?)C: \ Users \ User \ documents \ visual studio 2010 \ Projects \ Reports \ Reports.Web \ Services \ WebReportAggregateService.cs 53 108 Reports.Web – BentOnCoding 2010-11-03 17:42:50

+0

你試過我的解決方案嗎? – 2010-11-03 18:21:49

+0

事實證明,我在myReferrers變量上使用Take(10)方法導致了一些錯誤。 – BentOnCoding 2010-11-03 20:27:31