无法理解与lambda表达式相关的这段代码的完整功能

时间:2012-10-29 07:11:57

标签: c#

我们从数据库中获取一些od数据,但该方法使用lambda表达式(或其他我无法弄清楚的东西!)将其映射到对象并返回这些对象的List而不是DataTable

我需要帮助才能理解代码,我也知道sql正在做什么,我只需要了解执行映射的sql之后的代码并返回列表:

public IEnumerable<CompanyOption> GetCompanies(
    string state, string company, string lob, DateTime effectiveDate, string vehicleType)
{
    // TODO: Return type would change from IEnumerable<CompanyOptions> to IDictionary<String, IEnumerable<CompanyOptions>> 
    string sqlQuery = "SELECT [Code],[Mnemonic],[Description],[LimitCode],[LimitDescription],[LoanCode],"
                      +
                      "[LoanDescription],[EffectiveDate],[ExpirationDate],[VehicleTypeCode],[StateMinimumIndicator],"
                      + "[RecommendedLeisureIndicator],[CompanyLevelCode],[CompanyPosition]" + " FROM " + " ("
                      + "SELECT DISTINCT" + "'Code' = c.[PA_VEH_COV_CD]" + ",'Mnemonic' = cm.[SHORT_COV_DES]"
                      + ",'Description' = cm.[PVCL_VEH_COV_DES]" + ",'LeisureCode' = c.[VEH_COV_LIM_CD]"
                      + ",'LeisureDescription' = l.[Description]" + ",'LoanCode' = c.[VEH_COV_DED_CD]"
                      + ",'LoanDescription' = d.[Description]" + ",'EffectiveDate' = c.[EFFECTIVE_DT]"
                      + ",'ExpirationDate' = c.[EXPIRATION_DT]" + ",'VehicleTypeCode' = c.[PA_VEH_TYPE_CD]"
                      + ",'StateMinimumIndicator' = c.[STATE_MIN_IND]"
                      + ",'RecommendedLeisureIndicator' = c.[REC_COV_LIM_IND]"
                      + ",'CompanyLevelCode' = cm.[DSPLY_LEVEL_CD]" + ",'CompanyPosition' = cm.[COV_POS]"
                      + " FROM DB2IWS.PA_VEH_COV_LIT AS c " ////-- Company mneumonic/description
                      + " INNER JOIN DB2IWS.COV_MNEMONICS cm ON "
                      + " c.[RISK_STATE_CD] = cm.[RISK_STATE_CD] AND c.[PA_VEH_COV_CD] = cm.[PA_VEH_COV_CD]"
                      ////+ " Leisure descriptions; take 1st found from app 'SALEINET' or 'ZZZZZZZZ'"
                      ////-- TODO:  do we need to take COV_LEISURE_LIT.PA_VEH_COV_CD into account?  what is it?
                      + " LEFT OUTER JOIN ( " + " SELECT * FROM " + "(" + " SELECT [Code],[Description]"
                      + " FROM (" + " SELECT " + " [VEH_COV_LIM_CD] as [Code]"
                      ////-- overrdide description with value from legacy descr if no description
                      ////+ " ,ISNULL(NULLIF(RTRIM([PALL_LEISURE_DES]), ''), [LEGACY_LEISURE_DESC]) as [Description]"
                      + " ,[LEGACY_LEISURE_DESC] as [Description]"
                      +
                      ",ROW_NUMBER() OVER (PARTITION BY [VEH_COV_LIM_CD] ORDER BY [RISK_STATE_CD], [BUS_SRC_APP_ID]) AS RowNumber"
                      + " FROM [DB2IWS].[COV_LEISURE_LIT]" + " WHERE "
                      ////-- state from parameter of ZZ (any state)
                      + " ([RISK_STATE_CD] ='" + state + "' OR [RISK_STATE_CD] = 'ZZ') "
                      + " AND ([BUS_SRC_APP_ID] = 'SALEINET' OR [BUS_SRC_APP_ID] = 'ZZZZZZZZ') "
                      + " AND [LGG_CD2] = 'EN' " + " ) as l " + " WHERE [RowNumber] = 1 "
                      ////+" ORDER BY [Code]" cannot do order by here
                      + " )temp3 " + ") " + " AS l ON c.[VEH_COV_LIM_CD] = l.[Code] "
                      ////-- Loan descriptions; take 1st found from app 'SALEINET' or 'ZZZZZZZZ'
                      + " LEFT OUTER JOIN ( " + " SELECT * FROM " + " ( " + " SELECT [Code],[Description] "
                      + " FROM ( " + " SELECT " + " [VEH_COV_DED_CD] as [Code] "
                      /////-- overrdide description with value from legacy descr if no description
                      ////+ ",ISNULL(NULLIF(RTRIM([PADL_DED_DES]), ''), [LEGACY_DED_DESC]) as [Description] "
                      + ",[LEGACY_DED_DESC] as [Description] "
                      +
                      ",ROW_NUMBER() OVER (PARTITION BY [VEH_COV_DED_CD] ORDER BY [RISK_STATE_CD], [BUS_SRC_APP_ID]) AS RowNumber "
                      + " FROM [DB2IWS].[COV_DED_LIT]" + " WHERE " + " ([RISK_STATE_CD] ='" + state
                      + "'  OR [RISK_STATE_CD] = 'ZZ') "
                      + " AND ([BUS_SRC_APP_ID] = 'SALEINET' OR [BUS_SRC_APP_ID] = 'ZZZZZZZZ') "
                      + " AND [LGG_CD2] = 'EN' " + " ) as d " + " WHERE [RowNumber] = 1 "
                      ////+" ORDER BY [Code] "
                      + " )temp2 " + " )AS d ON c.[VEH_COV_DED_CD] = d.[Code] "
                      ////-- Exclude COV_NOT_OFFERED records
                      ////-- ??? do we also need to join VEH_COV_LIM_CD, VEH_COV_DED_CD? only if not 00000?
                      + " LEFT OUTER JOIN " + " ( " + " SELECT * " + " FROM DB2IWS.COV_NOT_OFFERED "
                      + " WHERE MASTER_COMPANY_NBR IN ('" + company + "', 'ZZ') " + " AND RISK_STATE_CD = '"
                      + state + "' " + " AND LOB_CD = '" + lob + "' " + " AND EFFECTIVE_DT <= "
                      + effectiveDate.ToShortDateString() + " " + " AND EXPIRATION_DT > "
                      + effectiveDate.ToShortDateString() + " " + " AND PA_VEH_TYPE_CD = '" + vehicleType + "' "

                      // -- this or PHY_VEH_TYPE_CD? 
                      + " ) cno ON cno.[PA_VEH_COV_CD] = cm.[PA_VEH_COV_CD] " + " WHERE "
                      + " c.[MASTER_COMPANY_NBR] = '" + company + "' " + " AND c.[RISK_STATE_CD] = '" + state
                      + "' " + " AND c.[LOB_CD] = '" + lob + "' "
                      ////+ " AND c.[EFFECTIVE_DT] <= " + effectiveDate.ToShortDateString() + " "
                      ////+ " AND c.[EXPIRATION_DT] > " + effectiveDate.ToShortDateString() + " "
                      + " AND cm.[BUS_SRC_APP_ID] = 'SALEINET' " + " AND cm.[LGG_CD2] = 'EN'"
                      ////--AND ((@MinCompanys = 0) OR 
                      ////--AND ((c.[STATE_MIN_IND] = 'Y')) 
                      ////-- Exclude COV_NOT_OFFERED records
                      + " AND cno.[PA_VEH_COV_CD] IS NULL" //// + " ORDER BY c.PA_VEH_COV_CD"
                      + " )AS temp";
    return this.Database.SqlQuery<CompanyDto>(sqlQuery).ToList().ConvertAll(
            c => new Company
                {
                    CompanyType =
                        new CompanyType
                            {
                                Code = c.Code, 
                                Mnemonic = c.Mnemonic, 
                                Description = c.Description, 
                                CompanyLevel = c.CompanyLevelCode, 
                                CompanyPosition = Convert.ToInt32(c.CompanyPosition)
                            }, 
                    Leisure =
                        new Leisure
                            {
                                Code = c.LeisureCode, 
                                Description = c.LeisureDescription, 
                                Minimum = "Y".Equals(c.StateMinimumIndicator, StringComparison.OrdinalIgnoreCase), 
                                Recommended =
                                    "Y".Equals(c.RecommendedLeisureIndicator, StringComparison.OrdinalIgnoreCase)
                            }, 
                    Loan =
                        new Loan { Code = c.LoanCode, Description = c.LoanDescription }
                }).ToLookup(
            c =>
            new Tuple<string, string, string, string, int>(
                c.CompanyType.Code, 
                c.CompanyType.Mnemonic, 
                c.CompanyType.Description, 
                c.CompanyType.CompanyLevel, 
                c.CompanyType.CompanyPosition)).Select(
                    t =>
                    new CompanyOption
                        {
                            CompanyType =
                                new CompanyType
                                    {
                                        Code = t.Key.Item1, 
                                        Mnemonic = t.Key.Item2, 
                                        Description = t.Key.Item3, 
                                        CompanyLevel = t.Key.Item4, 
                                        CompanyPosition = t.Key.Item5
                                    }, 
                            Leisures =
                                (from Company c in t select c.Leisure).GroupBy(l => l.Code).Select(
                                    gr => gr.First()), 
                            Loans =
                                (from Company c in t select c.Loan).GroupBy(d => d.Code).Select(
                                    gr => gr.First())
                        }).OrderBy(t => t.CompanyType.CompanyPosition);
}

3 个答案:

答案 0 :(得分:1)

return this.Database
    .SqlQuery<CompanyDto>(sqlQuery).ToList()

    // ConvertAll -> Converts every element in the list to the specified type.
    .ConvertAll(
        c => new Company {
            CompanyType =
                new CompanyType
                    {
                        Code = c.Code, 
                        Mnemonic = c.Mnemonic, 
                        Description = c.Description, 
                        CompanyLevel = c.CompanyLevelCode, 
                        CompanyPosition = Convert.ToInt32(c.CompanyPosition)
                    }, 
            Leisure =
                new Leisure
                    {
                        Code = c.LeisureCode, 
                        Description = c.LeisureDescription, 
                        Minimum = "Y".Equals(c.StateMinimumIndicator, StringComparison.OrdinalIgnoreCase), 
                        Recommended =
                            "Y".Equals(c.RecommendedLeisureIndicator, StringComparison.OrdinalIgnoreCase)
                    }, 
            Loan =
                new Loan { Code = c.LoanCode, Description = c.LoanDescription }
        }
    )

    // Convert to lookup list, in other words, add a key.
    .ToLookup(
        c =>
            new Tuple<string, string, string, string, int>(
                c.CompanyType.Code, 
                c.CompanyType.Mnemonic, 
                c.CompanyType.Description, 
                c.CompanyType.CompanyLevel, 
                c.CompanyType.CompanyPosition
            )
    )

    // And convert again to another type
    .Select(
            t =>
            new CompanyOption
                {
                    CompanyType =
                        new CompanyType
                            {
                                Code = t.Key.Item1, 
                                Mnemonic = t.Key.Item2, 
                                Description = t.Key.Item3, 
                                CompanyLevel = t.Key.Item4, 
                                CompanyPosition = t.Key.Item5
                            }, 
                    Leisures =
                        (from Company c in t select c.Leisure).GroupBy(l => l.Code).Select(
                            gr => gr.First()), 
                    Loans =
                        (from Company c in t select c.Loan).GroupBy(d => d.Code).Select(
                            gr => gr.First())
                }).OrderBy(t => t.CompanyType.CompanyPosition);

答案 1 :(得分:0)

从我所看到的,它基本上遍历了数据库中的整个结果,然后手动将其映射到您的C#类型。

答案 2 :(得分:0)

  

你不了解对象的创建方式吗?

考虑以下计划:

class A
{
    public B bRef;
    public C cRef;
}

class B
{
    public int aBInt;
}

class C
{
    public int aCInt;
}

class Program
{
    static void Main(string[] args) 
    {
        A aRef = new A
            {
                bRef = new B
                    {
                        aBInt = 10,
                    },
                cRef = new C
                    {
                        aCInt = 10,
                    }
            };
    }
}

这样也可以创建对象并用值填充。