2014-02-28 20 views
0

目前我有一个很多自动属性类看起来像下面的类。C#自动属性作为方法参数

class SQLiteTables 
{ 
    public class tbl_account_codes 
    { 
     [PrimaryKey, AutoIncrement] 
     public int ACCT_ID { get; set; } 
     public string ACCT_CODE { get; set; } 
     public string ACCT_DESC { get; set; } 
     public string FUND_CODE { get; set; } 
     public string ROR_FLAG { get; set; } 
     public string OR_FLAG { get; set; } 
     public string AR_FLAG { get; set; } 
     public DateTime CREATED_DATE { get; set; } 
     public string CREATED_BY { get; set; } 
     public Nullable<DateTime> LAST_MODIFIED_DATE { get; set; } 
     public string LAST_MODIFIED_BY { get; set; } 
     public string ACCT_STATUS { get; set; } 
    } 

    public class tbl_ack_receipt 
    { 
     [PrimaryKey, AutoIncrement] 
     public int AR_ID { get; set; } 
     public string TPAY_RECEIPT_NO { get; set; } 
     public string TPAY_SIG_ALGO { get; set; } 
     public string BFNS_CODE { get; set; } 
     public string TAXT_CODE { get; set; } 
     public string ACCT_CODE { get; set; } 
     public DateTime AR_PERIOD_COVERED { get; set; } 
     public Nullable<int> AR_QUARTER { get; set; } 
     public string AR_ASSESSMENT_NO { get; set; } 
     public Nullable<DateTime> AR_DUE_DATE { get; set; } 
     public string RFNP_CODE { get; set; } 
     public string RFNP_OTHER { get; set; } 
     public decimal AR_BASIC_TAX { get; set; } 
     public decimal AR_SURCHARGE { get; set; } 
     public decimal AR_INTEREST { get; set; } 
     public decimal AR_TOTAL_DUE { get; set; } 
     public decimal AR_TOTAL_PAID { get; set; } 
     public string MPAY_CODE { get; set; } 
     public string TYPEP_CODE { get; set; } 
     public string AR_REMARKS { get; set; } 
     public string AR_STATUS { get; set; } 
     public decimal AR_COMPROMISE { get; set; } 
    } 

    public class tbl_agency_codes 
    { 
     [PrimaryKey, AutoIncrement] 
     public int AGENCY_ID { get; set; } 
     public string AGENCY_CODE { get; set; } 
     public string AGENCY_DESC { get; set; } 
     public DateTime CREATED_DATE { get; set; } 
     public string CREATED_BY { get; set; } 
     public Nullable<DateTime> LAST_MODIFIED_DATE { get; set; } 
     public string LAST_MODIFIED_BY { get; set; } 
     public string AGENCY_STATUS { get; set; } 
    } 
} 

而且每次我想从一个类检索一个特定的属性,我需要这样做。

var qry = conn.Table<Tbl.tbl_agency_codes>().Where(x => x.AGENCY_CODE.StartsWith("0605")); 

不用说,写几十个这些是非常烦人的。所以我想知道是否可以简化它,并制定一个像这样的方法。

private void ThisMethod<T>(SomeProperty SomeProperty) 
{ 
    var qry = conn.Table<T>().Where(x => x.SomeProperty.StartsWith("Something")); 
} 

这可能吗?

+0

你可以使用反射 –

+0

您是否明白,如果重命名某个属性,您的代码将被无声地破坏? – Dennis

回答

0

方法:

private void ThisMethod<T>(Expression<Func<T, bool>> PredicateExp) 
{ 
    var qry = conn().Table<T>().Where(PredicateExp); 
} 

用法:

ThisMethod<Tbl.tbl_agency_codes>(x => x.AGENCY_CODE.StartsWith("0605")); 
1

是的,但有一些相当棘手的代码。

您可以通过的财产作为一个字符串,并反映出来,像x.GetType().Properties.Where(p=>p.Name == <prop name here>).GetValue(x)

或者你可以建立一个lambda,并传递它