2012-09-27 3 views
0

나는 그것이 신참 실수라고 확신하지만 나는 스스로 대답을 찾을 수 없다.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"로 하드 코딩 - 그리고 그건 내 문제가 해결되지 않습니다.

답변

2

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

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

감사 많은이 같은 표현을 생성하는 방법을 만들고, 내 문제를 해결! :) –

관련 문제