1

ビジュアル スタジオ 2008 | シルバーライト 3 | SQL Server 2005 | ドメイン サービス クラス | エンティティ データ モデル

FirstName、LastName、Semester、ProgramOfStudy、Columnなどの列を持つデータベーステーブル「Students」があります.......

目標は、指定されたSemesterおよびProgramofstudyのすべての学生の姓の最初の文字に基づいて、Total (姓の最初の文字でグループ化された学生の数) を返すことです。

LINQ クエリの作成に失敗したため、SQL ビューを使用しようと考えました。

  1. ビューを作成しました (17,000 行を 5000 行にグループ化)。
  2. ビューは、「LastNameStartsWith」、「Total」、「AcademicPeriod」、「ProgramCode」で構成されます。
  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 は報告されません。

return query; にブレークポイントが設定されている場合、エンティティ数は 24 と正しく報告されますが、各エンティティは同一です。LastNamesStartsWith = 'A' および合計 = 21。

フィルタリングは機能しているようですが、バグがあります。各エンティティは一意の AZ である必要があります。

.Where like soに追加すると


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

正しいリターンが生成されます。B、35、200890、BSED-ELED-ED

Linq の return 要素を使用すると、使用するわずかな要素が 1 つ得られます。LastNameStartsWith = "A" および Total = 21 です。ただし、B - 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);
                }
            }

これと同じ問題がここで尋ねられていますが、別のアプローチを使用しています。

4

1 に答える 1

0

Entity Framework を使用してビューを操作する場合は、エンティティ キーが正しく設定されていることを確認してください。ビューには主キーがないため、EF はキーを推測しようとしますが、多くの場合、正しくないため、これは見過ごされがちです。エンティティ キーが正しくないと、一貫性のない奇妙なクエリ結果が生じる可能性があります。

于 2011-03-13T03:14:31.627 に答える