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>
本文介绍了一种将Select方法作为一种实际的Map函数使用的方案,并通过具体示例展示了如何在不同类之间进行转换,包括单一对象及集合的转换过程。
977

被折叠的 条评论
为什么被折叠?



