DB から一部のデータを取得していますが、メソッドはラムダ式 (または私が理解できない他のもの!) を使用してオブジェクトにマップし、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);
}