查询的域服务类和实体数据模型一个SQL视图,使用LINQ(Querying a SQL View,

2019-10-19 20:36发布

Visual Studio 2008中| Silverlight的3 | SQL Server 2005中| 域服务类| 实体数据模型

我有一个数据库表“学生”的列名字,姓氏,学期,ProgramOfStudy,柱等.......

我们的目标是返回一个总计 (按姓氏的第一个字母分组学生人数)的基础上,在某一学期Programofstudy所有学生的姓氏的第一个字母。

我是不成功的编写LINQ查询,并想我会尝试使用SQL视图。

  1. 创建视图(17,000行分组到5000行)。
  2. 该意见由“LastNameStartsWith”,“总”,“AcademicPeriod”,“的程序代码”的。
  3. 增加了查看到的.edmx模型
  4. 写了下面的查询。

作为LINQ的;

public IQueryable GetVw_studentAlphabetSet(int cycleID, string progCode)
     {
          var query = from s in db.vw_studentAlphabetSet
          where ((s.programCode == progCode) && (s.academicPeriod == cycleID))
          select s;
          return query; 
}

当与填充MSSMS参数运行SQL视图筛选到25行报告发生的次数。 AZ,以X为有开始X.没有lastnames未报告

如果一个断点设置在返回查询,则它正确地报告的24实体数量,但每一个实体是相同的。 LastNamesStartsWith = 'A' 和总= 21。

因此,它似乎像过滤工作,但有一个bug。 每个实体应该是唯一的AZ。

如果我增加了。哪里像这样


where (s.programCode == progCode) && (s.academicPeriod == cycleID) && (s.LastNameStartsWith == "B");

生成一个正确的返回。 B,35,200890,BSED-ELED-ED

当使用LINQ的回报元素,我可以用一个可怜的元素。 LastNameStartsWith =“A”和总= 21,但再次乙 - 不返回Z和有总计。 在域服务类,它并报告该元素计为24他们都到哪里去了。 我应该得到即使他们是所有报告A和21 23更多的元素。

那么,有没有什么可以欣赏和LINQ工作时,特别应该知道吗?

在这里,设计师为实体模型


[assembly: global::System.Data.Objects.DataClasses.EdmSchemaAttribute()]

// Original file name:
// Generation date: 2/21/2010 10:25:00 PM
namespace sproc.Web
{

    /// 
    /// There are no comments for GLADYS_2010Entities in the schema.
    /// 
    public partial class GLADYS_2010Entities : global::System.Data.Objects.ObjectContext
    {
        /// 
        /// Initializes a new GLADYS_2010Entities object using the connection string found in the 'GLADYS_2010Entities' section of the application configuration file.
        /// 
        public GLADYS_2010Entities() : 
                base("name=GLADYS_2010Entities", "GLADYS_2010Entities")
        {
            this.OnContextCreated();
        }
        /// 
        /// Initialize a new GLADYS_2010Entities object.
        /// 
        public GLADYS_2010Entities(string connectionString) : 
                base(connectionString, "GLADYS_2010Entities")
        {
            this.OnContextCreated();
        }
        /// 
        /// Initialize a new GLADYS_2010Entities object.
        /// 
        public GLADYS_2010Entities(global::System.Data.EntityClient.EntityConnection connection) : 
                base(connection, "GLADYS_2010Entities")
        {
            this.OnContextCreated();
        }
        partial void OnContextCreated();
        /// 
        /// There are no comments for vw_studentAlphabet in the schema.
        /// 
        public global::System.Data.Objects.ObjectQuery vw_studentAlphabet
        {
            get
            {
                if ((this._vw_studentAlphabet == null))
                {
                    this._vw_studentAlphabet = base.CreateQuery("[vw_studentAlphabet]");
                }
                return this._vw_studentAlphabet;
            }
        }
        private global::System.Data.Objects.ObjectQuery _vw_studentAlphabet;
        /// 
        /// There are no comments for vw_studentAlphabet in the schema.
        /// 
        public void AddTovw_studentAlphabet(vw_studentAlphabet vw_studentAlphabet)
        {
            base.AddObject("vw_studentAlphabet", vw_studentAlphabet);
        }
    }
    /// 
    /// There are no comments for GLADYS_2010Model.vw_studentAlphabet in the schema.
    /// 
    /// 
    /// academicPeriod
    /// programCode
    /// 
    [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="GLADYS_2010Model", Name="vw_studentAlphabet")]
    [global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
    [global::System.Serializable()]
    public partial class vw_studentAlphabet : global::System.Data.Objects.DataClasses.EntityObject
    {
        /// 
        /// Create a new vw_studentAlphabet object.
        /// 
        /// Initial value of academicPeriod.
        /// Initial value of programCode.
        public static vw_studentAlphabet Createvw_studentAlphabet(int academicPeriod, string programCode)
        {
            vw_studentAlphabet vw_studentAlphabet = new vw_studentAlphabet();
            vw_studentAlphabet.academicPeriod = academicPeriod;
            vw_studentAlphabet.programCode = programCode;
            return vw_studentAlphabet;
        }
        /// 
        /// There are no comments for Property LastNameStartsWith in the schema.
        /// 
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public string LastNameStartsWith
        {
            get
            {
                return this._LastNameStartsWith;
            }
            set
            {
                this.OnLastNameStartsWithChanging(value);
                this.ReportPropertyChanging("LastNameStartsWith");
                this._LastNameStartsWith = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
                this.ReportPropertyChanged("LastNameStartsWith");
                this.OnLastNameStartsWithChanged();
            }
        }
        private string _LastNameStartsWith;
        partial void OnLastNameStartsWithChanging(string value);
        partial void OnLastNameStartsWithChanged();
        /// 
        /// There are no comments for Property Total in the schema.
        /// 
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public global::System.Nullable Total
        {
            get
            {
                return this._Total;
            }
            set
            {
                this.OnTotalChanging(value);
                this.ReportPropertyChanging("Total");
                this._Total = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
                this.ReportPropertyChanged("Total");
                this.OnTotalChanged();
            }
        }
        private global::System.Nullable _Total;
        partial void OnTotalChanging(global::System.Nullable value);
        partial void OnTotalChanged();
        /// 
        /// There are no comments for Property academicPeriod in the schema.
        /// 
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public int academicPeriod
        {
            get
            {
                return this._academicPeriod;
            }
            set
            {
                this.OnacademicPeriodChanging(value);
                this.ReportPropertyChanging("academicPeriod");
                this._academicPeriod = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
                this.ReportPropertyChanged("academicPeriod");
                this.OnacademicPeriodChanged();
            }
        }
        private int _academicPeriod;
        partial void OnacademicPeriodChanging(int value);
        partial void OnacademicPeriodChanged();
        /// 
        /// There are no comments for Property programCode in the schema.
        /// 
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public string programCode
        {
            get
            {
                return this._programCode;
            }
            set
            {
                this.OnprogramCodeChanging(value);
                this.ReportPropertyChanging("programCode");
                this._programCode = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, false);
                this.ReportPropertyChanged("programCode");
                this.OnprogramCodeChanged();
            }
        }
        private string _programCode;
        partial void OnprogramCodeChanging(string value);
        partial void OnprogramCodeChanged();
    }
}

这里是客户端代码调用域服务和实体模型。



using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Ria.Data;
using GLADYS.Web.GladysDataLayers;
using GLADYS.Web.GladysServices;


namespace GLADYS
    {
    public partial class uc_AlphabetFilter : UserControl
        {

        //data context for getting the data through the ria service.
        private dscStudents ctx = new dscStudents();




        public uc_AlphabetFilter()
            {
            InitializeComponent();
            //Loaded += new RoutedEventHandler(uc_AlphabetFilter_Loaded);
            ctx.Load(ctx.GetVw_studentAlphabetSetQuery(200980, "BSED-ELED-ED")).Completed += new EventHandler(uc_AlphabetFilter_Completed);            
            }





        void uc_AlphabetFilter_Completed(object sender, EventArgs e)
            {
            foreach (var q in ctx.vw_studentAlphabets)
                {
                uc_AlphaButton btn = new uc_AlphaButton();
                btn.pAlphaLetter = q.LastNameStartsWith;
                btn.PNumber = q.Total.Value;

                myWrapPanel.Children.Add(btn);
                }
            }

同样的问题被问在这里 ,而是通过不同的方法。

Answer 1:

当使用实体框架视图工作,确保实体按键被设置正确。 这往往被忽视,因为意见不具有主键EF试图推断出密钥,它往往是不正确。 具有不正确的实体键可能导致不一致和奇查询结果。



文章来源: Querying a SQL View, with Linq in a Domain Services Class and Entity Data Model
标签: sql linq views ria