c# - Linq's Select method as the Map function

If you comes from a functional programming language background, you will find that you are missing the Map function (literally) speakingly ..
However, the Select code is used as the de facto Map function. I have some examples, but I have to admit that sme of the examples are rather contrived. 

You can apply the Select as it is which is do a whole ranslation of the client data. while you can also provide certain predicate , which you can use as a mean to do filtering.
You have a class that is called PnL, and hat maps to the Table row data 

 

    internal class Data
    {
        #region Fields

        private string _book;
        private string _tradeType;
        private string _sector;
        private string _riskType;
        private string _ccy;
        private string _category;
        private string _tenure;
        private double _riskValue;
        private double _pnlValue;

        #endregion

        #region Public Properties

        [Column(Name = "Book", DataType = typeof(string))]
        public string Book
        {
            get { return _book; }
            private set { _book = value; }
        }

        [Column(Name = "TradeType", DataType = typeof(string))]
        public string TradeType
        {
            get { return _tradeType; }
            private set { _tradeType = value; }
        }

        [Column(Name = "Sector", DataType = typeof(string))]
        public string Sector
        {
            get { return _sector; }
            set { _sector = value; }
        }

        [Column(Name = "RiskType", DataType = typeof(string))]
        public string RiskType
        {
            get { return _riskType; }
            set { _riskType = value; }
        }

        [Column(Name = "Ccy", DataType = typeof(string))]
        public string Ccy
        {
            get { return _ccy; }
            set { _ccy = value; }
        }

        [Column(Name = "Category", DataType = typeof(string))]
        public string Category
        {
            get { return _category; }
            set { _category = value; }
        }

        [Column(Name = "Tenure", DataType = typeof(string))]
        public string Tenure
        {
            get { return _tenure; }
            set { _tenure = value; }
        }

        [Column(Name = "PnL", DataType = typeof(double))]
        public double PnLValue
        {
            get { return _pnlValue; }
            set { _pnlValue = value; }
        }

        [Column(Name = "Risk", DataType = typeof(double))]
        public double RiskValue
        {
            get { return _riskValue; }
            set { _riskValue = value; }
        }

        #endregion
    }

 

and then you have a class where you used as data model to  process. and the PnLDataModel looks like this:

[ObjectId("Key")]
    internal class DataModel : ICloneable
    {
        #region Fields

        private Tuple<string, string, string, string, string, string, string> _key;
        private string _book;
        private string _tradeType;
        private string _sector;
        private string _riskType;
        private string _ccy;
        private string _category;
        private string _tenure;
        private double _riskValue;
        private double _pnlValue;

        #endregion

        #region Constructors

        public DataModel(
            string book,
            string tradeType,
            string sector,
            string riskType,
            string ccy,
            string category,
            string tenure)
        {
            Book = book;
            TradeType = tradeType;
            Sector = sector;
            RiskType = riskType;
            Ccy = ccy;
            Category = category;
            Tenure = tenure;
            _key = new Tuple<string, string, string, string, string, string, string>(
                book,
                tradeType,
                sector,
                riskType,
                ccy,
                category,
                tenure);
        }

        #endregion

        #region Public Properties

        public Tuple<string, string, string, string, string, string, string> Key
        {
            get
            {
                return _key;
            }
        }

        public string Book
        {
            get { return _book; }
            private set { _book = value; }
        }

        public string TradeType
        {
            get { return _tradeType; }
            private set { _tradeType = value; }
        }

        public string Sector
        {
            get { return _sector; }
            private set { _sector = value; }
        }

        public string RiskType
        {
            get { return _riskType; }
            private set { _riskType = value; }
        }

        public string Ccy
        {
            get { return _ccy; }
            private set { _ccy = value; }
        }

        public string Category
        {
            get { return _category; }
            private set { _category = value; }
        }

        public string Tenure
        {
            get { return _tenure; }
            private set { _tenure = value; }
        }

        public double PnLValue
        {
            get { return _pnlValue; }
            set { _pnlValue = value; }
        }

        public double RiskValue
        {
            get { return _riskValue; }
            set { _riskValue = value; }
        }

        #endregion

        #region ICloneable

        public object Clone()
        {
            return new DataModel(
                Book,
                TradeType,
                Sector,
                RiskType,
                Ccy,
                Category,
                Tenure)
            {
                PnLValue = PnLValue,
                RiskValue = RiskValue
            };
        }

        #endregion
    }

 and you will need to convert from one type to another, so you can do this:

private DataModel DataProviderToModel(Data Data)
        {
            return new DataModel(
                Data.Book,
                Data.TradeType,
                Data.Sector,
                Data.RiskType,
                Data.Ccy,
                Data.Category,
                Data.Tenure)
                       {
                           PnLValue = Data.PnLValue,
                           RiskValue = Data.RiskValue
                       };
        }



        private IEnumerable<DataModel> DataProviderToModel(IEnumerable<Data> Datas)
        {
            return Datas.Select(DataProviderToModel);
        }

Basically you defined a MapMethod and the used that to apply on IEnumerable<Source> to IEnumerable<Target>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值