0

データベース:データベースには、IDがintでコードが。の3つのテーブルがありますstring datatype。例外的です(countryIdも文字列です)。

  • 国(countryId、countryCode ...)
  • Company(companyId、countryId、year、companyCode ...)
  • Center(centerId、companyId、year、centerCode ...)

センターのコードを知っている2つの異なるに属する2つの特定のセンターを取得しようとしています。

User Action :ユーザーは、ランダムな値(CORP)が指定されている場所に追加された追加のidemであるからオプション(Special)を選択します。 DropDownListListItem

_DropDownListCountry.Items.Insert(1, new ListItem("Special", "CORP"));

予想されるユーザーが特定のの_DropDownListCountryから上記のオプションを選択すると、2つのアイテムが別のアイテムDropDownList(_DropDownListCenter)に入力される必要があります。アイテムはリストに含まれている必要があります(例:1111および2222)。ごとにセンター企業との関係があります。

問題:データベースには2つのセンターがありますが、が異なるため、結果は(1111、2222、2222)と表示されます。||これは、クエリで使用したときに発生しLINQます。しかし、なぜそれが起こっているのか(コード的に)わかりません。どんな助けでも本当にありがたいです。

コード

public static IEnumerable<Center> RetriveCenterssByYear(short year)
    {
        List<Center> centers;
        using (var context = new crEntities())
        {
            centers = (from pc in context.Centers
                             join company in context.Companies on pc.CompanyID equals company.CompanyID
                             join co in context.Countries on company.CountryID equals co.CountryID
                             where pc.Year == year 
                                     && pc.CompanyID == company.CompanyID 
                                    && company.CountryID.Equals(co.CountryID)
                                    &&  pc.centerCode.Contains("1111")
                                    ||  pc.centerCode.Contains("2222")

                             select pc).ToList();

        }
        return centers;

    }
4

1 に答える 1

1

角かっこを追加します

pc.centerCode.Contains("1111")
||  pc.centerCode.Contains("2222")

だからあなたの状態は次のようになります

pc.Year == year 
&& pc.CompanyID == company.CompanyID 
&& company.CountryID.Equals(co.CountryID)
&&  (pc.centerCode.Contains("1111")
||  pc.centerCode.Contains("2222"))

論理演算子の優先度は同じであるため、クエリは現在次のように解釈されます。

(pc.Year == year 
&& pc.CompanyID == company.CompanyID 
&& company.CountryID.Equals(co.CountryID)
&&  pc.centerCode.Contains("1111"))
||  pc.centerCode.Contains("2222")
于 2013-03-13T12:38:20.030 に答える