简体   繁体   中英

Can I put this in separate class?

I have this repetitive piece of code:

var expCodes = (from cpdet in _dataContextOrders.CodeProfileDetails
                join cpd in _dataContextOrders.CodeProfileDefinitions on cpdet.CodeProfileDefinitionID equals cpd.ID
                join cp in _dataContextOrders.CodeProfiles on cpd.CodeProfileID equals cp.ID
                join cc in _dataContextOrders.FMSCostCentres on cpdet.CostCentreID equals cc.ID
                join ec in _dataContextOrders.FMSExpenseCodes on cpdet.ExpenseCodeID equals ec.ID
                where cp.ID == Convert.ToInt32(intCostCodeProfileId)
                                  && cpdet.CostCentreID == Convert.ToInt32(intCostCentreSelected)
                                  && ec.Active == true
                select new
                {
                    ec.ID,
                    ec.CostCentreID,
                    ExpenseCodeExternalRef = ec.ExternalRef,
                    ExpenseCodeDescription = ec.Description,
                    displayExpenseCode = ec.ExternalRef + " " + ec.Description
                 }).Distinct().OrderBy(ec => ec.displayExpenseCode);

ddlExpCode1.DataSource = expCodes;
ddlExpCode1.DataTextField = "displayExpenseCode";
ddlExpCode1.DataValueField = "ID";

What I would like to do is to put it into a Class on its own, as we did before LinqToSql, that I can call from my aspx.cs page, using the 2 parameters, intCostCodeProfileId and intCostCodeProfileId and it will return the data for the Drop Down List.

How can I do this?

You have to create a model class that matches the properties in your select statement (ID, CostCentreId etc.). Then modify the select new { to select new FreshlyCreatedModelClass() { .

The only way to return an anonymous type from your method is to use either IEnumerable<object> or IEnumerable<dynamic> neither which should be recommended in this scenario. Your method should return IEnumerable<FreshlyCreatedModelClass> (or IQueryable<FreshlyCreatedModelClass> could be used if you need to build the query further).

After you have sorted the model class, you can simply move the code to a separate method.

Create a normal class (eg Connection.cs) and add some code like this:

public class MyDataSourceReturner
{
    public static Object retDatasource(Object _dataContextOrders, int intCostCodeProfileId){
    var expCodes = (from cpdet in _dataContextOrders.CodeProfileDetails
            join cpd in _dataContextOrders.CodeProfileDefinitions on cpdet.CodeProfileDefinitionID equals cpd.ID
            join cp in _dataContextOrders.CodeProfiles on cpd.CodeProfileID equals cp.ID
            join cc in _dataContextOrders.FMSCostCentres on cpdet.CostCentreID equals cc.ID
            join ec in _dataContextOrders.FMSExpenseCodes on cpdet.ExpenseCodeID equals ec.ID
            where cp.ID == Convert.ToInt32(intCostCodeProfileId)
                              && cpdet.CostCentreID == Convert.ToInt32(intCostCentreSelected)
                              && ec.Active == true
            select new
            {
                ec.ID,
                ec.CostCentreID,
                ExpenseCodeExternalRef = ec.ExternalRef,
                ExpenseCodeDescription = ec.Description,
                displayExpenseCode = ec.ExternalRef + " " + ec.Description
             }).Distinct().OrderBy(ec => ec.displayExpenseCode);

    return expCodes;
    }
}

Afterwards you can use this code like this:

ddlExpCode1.DataSource = MyDataSourceReturner.retDatasource(_dataContextOrders, 5);

and receive your DataSource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM