0

初歩的なミスだと思いますが、自分では答えが見つかりません。

public class Database : DataContext 
{
    public Table<Record> RecordTable;
    public Database(string connection) : base(connection) {}
}

[Table(Name = "RecordsTable")]
public class Record
{
    [Column( IsPrimaryKey = true, CanBeNull = false)]
    public int codedID;
}

// inside some class
private void ReadTestData(Database openedDatabase, int expectedValue)
{
    Table<Record> rec = openedDatabase.GetTable<Record>();
    var q =
        from a in rec
        where (GetMonth == expectedValue)   // <--- that line doesn't work
        select a;

    foreach (var b in q) { System.Console.WriteLine("something"); }
}

static Expression<Func<Record, int>> GetMonth = a => a.codedID/10000;

public static int DecodeMonth(int codedID)
{
    int month = codedID/10000;
    //(...)
    return month;
}

DecodeMonth関数を呼び出して、その戻り値を expectedValue と比較したいと思います。どうすればいいですか?

私はいくつかの調査を行い、そのようなコードを実行することができました:

var q = openedDatabase.RecordTable.Where(GetMonthBool);

static Expression<Func<Record, bool>> GetMonthBool = a => (a.codedID/10000 == 1);

しかし、expectedValue は "1" としてハードコードされています - それで問題は解決しません。

4

1 に答える 1

2

このような式を作成するメソッドを作成します

private Expression<Func<Record, bool>> GetMonthBoolFunc(int value)
{
    return a => (a.codedID/10000 == value); 
}

var q = openedDatabase.RecordTable.Where(GetMonthBoolFunc(1)); 
于 2012-09-27T19:38:45.870 に答える