【问题标题】:entity framework / linq query problem实体框架/linq查询问题
【发布时间】:2010-11-03 17:10:57
【问题描述】:

因此,我正在尝试解决我的 silverlight 报告应用程序中缺少存储过程支持的问题,并且我的 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

我收到访问、自定义、潜在客户、访问者、推荐人和推荐人 ID 的此错误。

任何帮助将不胜感激 =D

【问题讨论】:

  • 您可以从在您的问题中实际包含一个错误开始...

标签: c# entity-framework silverlight-4.0 wcf-ria-services


【解决方案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; }

}

【讨论】:

  • 为所有 g.Key 属性生成此错误。错误 2“字符串”不包含“客户”的定义,并且找不到接受“字符串”类型的第一个参数的扩展方法“客户”(您是否缺少 using 指令或程序集引用?) C:\Users \User\documents\visual studio 2010\Projects\Reports\Reports.Web\Services\WebReportAggregateService.cs 53 108 Reports.Web
  • 原来是我在 myReferrers 变量上使用 Take(10) 方法导致了一些错误。
【解决方案2】:

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 }

【讨论】:

  • 我的组键是一个字符串,从你的句子的上下文中我假设组键需要是一个 int?
  • @Robotsushi,不,这会产生类似的错误,试图在 int 上调用 Customers 方法。请查看我编辑的答案 - 我不完全确定 ID 和名称的来源,但我认为这很接近。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多