0

少し抽象的で申し訳ありません。私は開発の初期段階にいます。

私は2つのオブジェクトタイプを持っています:

  1. 一連のユーザー定義条件を保存する必要があるオブジェクト。
  2. 最初のオブジェクトで定義された条件の 0 個以上に一致するオブジェクト。

これがどのように実行されるかの簡単な例です。

  1. ユーザーがタイプ 2 のオブジェクトをいくつか作成し、それらをコレクションに追加します。
  2. 次に、ユーザーはタイプ 1 のオブジェクトを作成し、それにいくつかの条件を割り当てます。
  3. システムはオブジェクト タイプ 1 の条件を使用して、各オブジェクトが一致する条件のパーセンテージでソートされたタイプ 2 のオブジェクトのリストを生成します。

予想される出力のサンプルを次に示します。

Conditions                                            List
Quantity >= 2                                         Object5 100%
Value < 2                                             Object2  75%
Category = "Blah"                                     Object4  50%
Quality > 5                                           Object1  25%
                                                      Object3   0%

各条件の最初のオペランドはプロパティの名前で、2 番目のオペランドはそのプロパティの値です。

どうすればこれを達成できますか?

私の最初の考えは、クエリ言語に似ているということです。それらが DB テーブルのレコードである場合、条件ごとに SQL 文字列をつなぎ合わせ、各クエリを実行して、各レコード ID がクエリ結果に表示された回数を数えることができます。しかし、これらは単純な古い C# オブジェクトです。オブジェクトの永続化に何を使用するかはまだ決めていないので、現時点ではデータベース エンジンに縛り付けたくありません。助言がありますか?

4

4 に答える 4

1

これは、LINQが使用したいもののように思えます。特に、永続ストアが最終的に選択される場合は、 LINQ To ObjectsLINQ To SQLを調べます。

于 2010-10-28T04:29:44.037 に答える
0

また、 SQLクエリを直接実行する方法(LINQ to SQL)に関する記事もあります。

Northwnd db = new Northwnd(@"c:\northwnd.mdf");
IEnumerable<Customer> results = db.ExecuteQuery<Customer>
(@"SELECT c1.custid as CustomerID, c2.custName as ContactName
    FROM customer1 as c1, customer2 as c2
    WHERE c1.custid = c2.custid"
);
于 2010-10-28T04:36:00.183 に答える
0

文字列で LINQ-to-Objects を使用した実装例を次に示します。

var conditions = new Func<string, bool>[]
{
    s => s.Length < 4,                  // less than 4 characters
    s => s.Count(c => c == 'o') > 2,    // more than 2 'o's
    s => s == "fooo",                   // is "fooo"
    s => s.Contains('b'),               // contains 'b'
};
var words = new[] { "foo", "fooo", "foooo", "bar", "barr", "bazooo" };
var query = words.Select(Word => new
                                 {
                                     Word,
                                     Percentage = conditions.Average(c => c(Word) ? 1M : 0M)
                                 })
                 .Where(p => p.Percentage > 0)          // keep words matches some conditions
                 .OrderByDescending(p => p.Percentage)  // order by the percentage
                 .ThenBy(p => p.Word);                  // then order in alphabetical order

振り返ってみると、この例は LINQ-to-SQL でも機能する可能性があると思います (conditions配列を微調整することで)。

これは簡単な例にすぎませんが、アイデアをさらに拡張することができます。

于 2010-10-28T05:01:26.233 に答える
0

これは、仕様パターンの修正版で行うことができます。結果をパーセンテージで表すインターフェースから始めます。

public interface ISpecification<T>
{
    double GetPercentSatisfiedBy(T target);
}

次に、任意の条件を適用する仕様を作成します。

public sealed class Specification<T> : ISpecification<T>
{
    private readonly Func<T, bool> _predicate;

    public Specification(Func<T, bool> predicate)
    {
        _predicate = predicate;
    }

    public double GetPercentSatisfiedBy(T target)
    {
        return _predicate(target) ? 1 : 0;
    }
}

ここで、他の仕様の結果を線形結合する仕様を作成します。

public sealed class CompositeSpecification<T> : ISpecification<T>
{
    private readonly IList<ISpecification<T>> _specifications;

    public CompositeSpecification(params ISpecification<T>[] specifications)
    {
        _specifications = specifications.ToList();
    }

    public double GetPercentSatisfiedBy(T target)
    {
        return _specifications.Average(
            specification => specification.GetPercentSatisfiedBy(target));
    }
}

最後に、必要な条件をすべて含む仕様を作成し、それをFooオブジェクトのリストに適用します。

var specification = new CompositeSpecification<Foo>(
    new Specification<Foo>(foo => foo.Quantity >= 2),
    new Specification<Foo>(foo => foo.Value < 2),
    new Specification<Foo>(foo => foo.Category == "Blah"),
    new Specification<Foo>(foo => foo.Quality > 5));

var foos = new List<Foo> { ... };

var results =
    from foo in foos
    let percentSatisfied = specification.GetPercentSatisfiedBy(foo)
    orderby percentSatisfied descending
    select new
    {
        Foo = foo,
        PercentSatisfied = percentSatisfied
    };

この設計は、任意の複雑さの仕様をサポートします。

于 2010-10-28T05:21:47.580 に答える