0

リストから文字列を見つけようとしています....機能していないList<string>ようです..

    List<string> c = new List<string>();
    c.Add("John Doe"));
    c.Add("Erich Schulz"));

// Criterion クラスに問題があると思いますか? ここに私のクラス構造があります:

public class Criterion
{
    public Criterion(String propertyName, object value)
    {
        this.PropertyName = propertyName;
        this.Value = value;
    }
}

//here is the method...
public static List<Criterion> LoadNames()
{
    List<Criterion> c = new List<Criterion>();
    c.Add(new Criterion("Name1", "John Doe"));
    c.Add(new Criterion("Name2", "Erich Schulz"));
    return c;
}

これを機能させようとしているコードは次のとおりです。

bool isExists = LoadNames.Any(s=> "Erich Schulz".Contains(s));

エラー:

「Any」の定義と最適な拡張メソッドのオーバーロードが含まれていません'System.Linq.Enumerable.Any<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)' has some invalid arguments

4

2 に答える 2

2

を呼び出すと、文字列.Contains(s)sはなく、Criterion. を使用し.Contains(s.propertyName)ます。

bool isExists = LoadNames().Any(s=> "Erich Schulz".Contains(s.PropertyName));

また、メソッドとして LoadNames を使用しているため、最初に実行する必要があります。

于 2013-05-06T21:07:10.510 に答える
0

文字列を Criterion オブジェクトと比較しようとしていますが、うまくいきません。

固定コードは次のとおりです。

bool isExists = LoadNames.Any(criterion => String.Equals(criterion.PropertyName, "Erich Schulz", StringComparison.OrdinalIgnoreCase));
于 2013-05-06T21:07:40.357 に答える