0

テーブルからの選択で文字列をどのように使用しますか? 例:

このクエリを変換します。

 DBEntities MyDB = new DBEntities();
        var Query1 = from P in MyDB.Per
        where P.IDRANK == 2
        select P;
to:


 string strquery = "where P.IDRANK == 2";
    DBEntities MyDB = new DBEntities();
    var Query1 = from P in MyDB.Per
         strquery 
    select P;
4

3 に答える 3

0

各テーブルには、多くの関数を書いています。これらを 1 つの関数に変換する必要があります。
例:テーブルごとに2つの関数があります

private void SelectByRank(int Rank)
        {
            DBEntities MyDB = new DBEntities();
            var Query1 = from P in MyDB.Per
                         where P.IDRANK == Rank
                         select P;
        }
        private void SelectByRankANDBlah(int Rank, string Blah)
        {
            DBEntities MyDB = new DBEntities();
            var Query1 = from P in MyDB.Per
                         where P.IDRANK == Rank && P.BLAH == Blah
                         select P;
        }     

今、私は 1 つの関数に変換します。例えば

private void Select(string strKind,int Rank, string Blah)
        {
            var strWhere ="";
            if(strKind == "RANK")
            {
                strWhere  = "where P.IDRANK ==" + Rank;
            }
            else 
            {
                strWhere  = "where P.IDRANK == " + Rank + " && P.BLAH == " + Blah;
            }

            DBEntities MyDB = new DBEntities();
            var Query1 = from P in MyDB.Per
                            strWhere 
                         select P;
        }

わかった?

于 2013-08-01T20:06:10.323 に答える
0

私はこのコードを使用します

using (var context = new DBEntities())
            {
                System.Data.Objects.ObjectQuery<User> contacts = context.Users.Where("it.FirstName = @fname", new System.Data.Objects.ObjectParameter("fname", "Arash"));
                List<User> items = contacts.ToList();
            }
于 2013-08-04T18:14:52.227 に答える