Пример #1
0
        /// <summary>
        /// 从DataTable中获取实体列表
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private List <DAL.Cargos> GetModelFromDataTable(DataTable dt, out string msg)
        {
            List <DAL.Cargos> list = new List <DAL.Cargos>();

            msg = "";

            //接口协议文档中定义的字段
            Dictionary <string, string> dataFieldNameDic = new Dictionary <string, string>();

            /*
             * CargoCode	商品编码
             * CargoName	商品名称
             * CargoSpec	规格
             * CargoModel	型号
             * PriceWholeSale	批发价
             * PriceGoldCard	金卡会员价
             * PriceSilverCard	银卡会员价
             * PriceWhiteGoldCard	白金卡会员价
             * PriceReference	参考售价
             *
             */
            dataFieldNameDic.Add("CargoCode", "商品编码");
            dataFieldNameDic.Add("CargoName", "商品名称");
            dataFieldNameDic.Add("PriceWholeSale", "批发价");
            dataFieldNameDic.Add("PriceGoldCard", "金卡会员价");
            dataFieldNameDic.Add("PriceSilverCard", "银卡会员价");
            dataFieldNameDic.Add("PriceWhiteGoldCard", "白金卡会员价");
            dataFieldNameDic.Add("PriceReference", "参考售价");

            if (dt.Rows.Count == 0)
            {
                msg = "用友系统返回数据集中无数据!";
                return(new List <Cargos>());
            }

            StringBuilder errorColName = new StringBuilder();

            //检查数据集中是否存在指定字段
            foreach (KeyValuePair <string, string> kvp in dataFieldNameDic)
            {
                if (dt.Columns.Contains(kvp.Key) == false)
                {
                    errorColName.Append(Environment.NewLine);
                    errorColName.Append(kvp.Value);
                    errorColName.Append("-");
                    errorColName.Append(kvp.Key);
                }
            }
            if (errorColName.Length > 0)
            {
                errorColName.Insert(0, "用友系统返回的数据集中未包含如下字段,不能进行有效解析!");
                msg = errorColName.ToString();
                return(new List <Cargos>());;
            }

            //遍历数据集创建实体
            foreach (DataRow dr in dt.Rows)
            {
                Cargos newModel = new Cargos();

                newModel.CargoCode = DataCheckHelper.GetCellString(dr["CargoCode"]);
                newModel.CargoName = DataCheckHelper.GetCellString(dr["CargoName"]);
                newModel.Price1    = DataCheckHelper.GetCellDecimal(dr["PriceWholeSale"]);
                newModel.Price2    = DataCheckHelper.GetCellDecimal(dr["PriceGoldCard"]);
                newModel.Price3    = DataCheckHelper.GetCellDecimal(dr["PriceSilverCard"]);
                newModel.Price4    = DataCheckHelper.GetCellDecimal(dr["PriceWhiteGoldCard"]);
                newModel.Price5    = DataCheckHelper.GetCellDecimal(dr["PriceReference"]);

                List <Cargos> existWareHouse = (from r in list where r.CargoCode == newModel.CargoCode select r).ToList <Cargos>();
                if (existWareHouse == null || existWareHouse.Count == 0)//过滤重复数据
                {
                    list.Add(newModel);
                }
            }

            return(list);
        }