2010-11-25 9 views
0

3つの異なる文字列入力を持つリストを照会する必要があります。入力文字列の形式はstring1|string2|string3です。だから私はそれらを分割する必要があるsplit('|')前に。3つの異なるリストのANDQクエリ

今度は、文字列を分割して得られた3つの異なる配列(country[],state[]city[])の値を比較することによってリスト(Locations<Location>())を照会する必要があります。

例は次のように私はそれぞれのLINQクエリを持っている:

IEumerable<Location> loc = Locations.Where(lc=> Country.Any(entry => lc.CountryName. StartsWith(entry,StringComparison.OrdinalIgnoreCase))); 

国と一致するために、私は国家の文字列入力が空である場合、状態のチェックをスキップする必要があります。だから私の解決策は:

  1. クエリーカントリーを最初にしていました。文字列が空の場合は、元の入力リストを返します。

  2. 上記のステップから得られた結果のステートの問い合わせ。文字列が空の場合は、元の入力リストを返します。

  3. 上記の手順で得られた結果に関するCityのクエリ。文字列が空の場合は、元の入力リストを返します。

上記の問題を考慮して、3つすべてを1つのLINQクエリに組み合わせる方法はありますか。私ははっきりしていたと思う。つまり に期待される出力

private IList<Location> FilterLocation(
    IList<Location> locations, LocationRequest Request) 
{ 
    locations = FindCountry(locations, Request.Countries); 
    locations = FindStatenames(locations, Request.States); 
    locations =FindCitynames(locations, Request.Cities); 

    return locations; 
} 

private IList<Location> FindCountry(
    IList<Location> locations, string Countrynames) 
{ 
    if (!string.IsNullOrEmpty(Countrynames)) 
    {   
     string[] Country= Countrynames.Split('|'); 
     IEnumerable<Study> result = locations.Where(lc=> Country.Any(entry => lc.CountryName.StartsWith(entry,StringComparison.OrdinalIgnoreCase))); 

     return result.ToList(); 
    } 
    return locations; 
} 

private IList<Location> FindStatenames(
    IList<Location> locations, string Statenames) 
{    
    if (!string.IsNullOrEmpty(Statenames)) 
    { 
     string[] States =Statenames.Split('|'); 
     IEnumerable<Study> result = locations.Where(lc=> States.Any(entry => lc.StateName.StartsWith(entry,StringComparison.OrdinalIgnoreCase))); 

     return result.ToList();              
    } 
    return locations; 
} 

private IList<Location> FindCitynames(
    IList<Location> locations, string Citynames) 
{ 
    if (!string.IsNullOrEmpty(Citynames)) 
    { 
     string[] Cities = Citynames.Split('|'); 
     IEnumerable<Study> result = locations.Where(lc=> Cities.Any(entry => lc.CityName.StartsWith(entry,StringComparison.OrdinalIgnoreCase))); 

     return result.ToList(); 
    } 
    return locations; 
} 

サンプル出力

 First Sample 
     Input 
     Countrynames = "Country1|Country2|Country3" 
     Statenames = "State1|State2|State3" 
     Citynames = "City1|City2|City3" 

     Output 
     Match Location with Country2,State1,City3 

    Second Sample 
     Input 
     Countrynames = null 
     Statenames = "State1|State2|State3" 
     Citynames = "City1|City2|City3" 
     Output 
     Match Location with Country2,State1,City3   
     Match Location with Country10,State1,City3 
     Match Location with Country15,State1,City3 
     Match Location with Country8,State1,City3 

現在のコードはCountrynames = nullの場合、検索に含めないでください。 Statenames = nullの場合検索に含めないでください。 Citynames = nullの場合検索に含めないでください。

ありがとうございます。

答えて

0

あなたは完全に明確ではないですが、あなたが望むように、それはこのようなものに聞こえる:

IEumerable<Location> loc = Locations.Where(lc => 
    (CountryList == "" || Country.Any(entry => lc.CountryName.StartsWith(entry,StringComparison.OrdinalIgnoreCase))) 
&& (StateList == "" || State.Any(entry => lc.StateName.StartsWith(entry,StringComparison.OrdinalIgnoreCase))) 
&& (CityList == "" || City.Any(entry => lc.CityName.StartsWith(entry,StringComparison.OrdinalIgnoreCase)))); 

はここにあなたの機能の私の書き換えです:

private IList<Location> FilterLocation( 
    IList<Location> locations, LocationRequest request) 
{ 
    string[] countryList, stateList, cityList; 
    if (!string.IsNullOrEmpty(request.Country)) 
     countryList = request.Country.Split("|"); 
    if (!string.IsNullOrEmpty(request.State)) 
     stateList = request.State.Split("|"); 
    if (!string.IsNullOrEmpty(request.City)) 
     cityList = request.City.Split("|"); 

    return locations.Where(lc => 
     (countryList == null || countryList.Any(entry => lc.CountryName.StartsWith(entry,StringComparison.OrdinalIgnoreCase))) 
    && (stateList == null || stateList.Any(entry => lc.StateName.StartsWith(entry,StringComparison.OrdinalIgnoreCase))) 
    && (cityList == null || cityList.Any(entry => lc.CityName.StartsWith(entry,StringComparison.OrdinalIgnoreCase)))) 
     .ToList(); 
} 
+0

仮定する郡は、この空の場合、私は信じていますCountry = "someCountry"、State = "someState"、City = "SomeCity"のようなエントリではなく、Country = ""、State = "someState"、City = "SomeCity"のようなエントリと一致します。 – fireBand

+0

国が空の場合、私はそれを探してスキップする必要があります。 – fireBand

+0

fireBand:私の答えが正しいかどうかは分かりません。より明確に書いてください。 – Gabe

関連する問題