簡體   English   中英

LINQ“where”帶參數

[英]LINQ “where” with parameter

我確定這是新手的錯誤,但我自己找不到答案。

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” - 這並不能解決我的問題。

創建一個創建這樣的表達式的方法

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

var q = openedDatabase.RecordTable.Where(GetMonthBoolFunc(1)); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM