ELM
ELM

Reputation: 529

C# Automatic Property as Method Parameter

Currently I have a class with a lot of automatic property classes looking like the ones below.

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; }
    }
}

And everytime I want to retrieve a specific property from a class, i need to do this.

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

Needless to say, writing dozens of these is very tiresome. So I was wondering if it's possible to simplify it and make a method something like this.

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

Is this possible?

Upvotes: 0

Views: 362

Answers (2)

ELM
ELM

Reputation: 529

Method:

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

Usage:

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

Upvotes: 0

Iain Ballard
Iain Ballard

Reputation: 4828

Yes, but with some fairly tricky code.

You can either pass the name of the property as a string and reflect it out, something like x.GetType().Properties.Where(p=>p.Name == <prop name here>).GetValue(x)

Or you can build a lambda and pass it in

Upvotes: 1

Related Questions