Hi friends
I have a list of object in which there is a filed called market value. i need to sort the list in descending order based upon the market value. after sorting i need to take first 10 values from the list.
But there is some extra condition while picking up the first 10.
sorting order should be positive, negative, zero,
and in negative it should be -1,-2,-3...
How can i achieve this in an easy way. is there any built in functionality.
public static List<GetRequestTopPositions> GetRequestTopPositions(string xmlstring)
{
List<GetRequestTopPositions> list_accr = new List<GetRequestTopPositions>();
XDocument doc = XDocument.Parse(xmlstring);
var result = doc.Descendants("PositionGroup")
.Descendants("PositionSummary")
.Descendants("PositionDetail")
.Where(item =>
{
string sd = (string)item.Parent.Parent.Element("IssueType") ?? "";
return sd != null && sd != "2";
})
.Select(item => new GetRequestTopPositions
{
IssueType = (string)item.Parent.Parent.Element("IssueType"),
Symbol = (string)item.Parent.Element("Symbol"),
Cusip = (string)item.Parent.Element("Cusip"),
ClosingPrice = (double?)item.Parent.Element("ClosingPrice") ?? 0D,
LatestPrice = (double?)item.Parent.Element("LatestPrice") ?? 0D,
ChangeInPrice = (double?)item.Parent.Element("ChangeInPrice") ?? 0D,
UnitPrice = (double?)item.Parent.Element("UnitPrice") ?? 0D,
PriceFactor = (double?)item.Parent.Element("PriceFactor") ?? 0D,
MSDWSecurityCode = (string)item.Parent.Element("MSDWSecurityCode"),
SecurityDescription = (string)item.Parent.Element("SecurityDescription"),
Office = (string)item.Element("Office"),
Account = (string)item.Element("Account"),
KeyAccount = (string)item.Element("KeyAccount"),
Quantity = (double?)item.Element("Quantity") ?? 0D,
MarketValue = (double?)item.Element("MarketValue") ?? 0D,
ChangeInMarketValue = (double?)item.Element("ChangeInMarketValue") ?? 0D
})
.ToList()
.GroupBy(g => new { g.SecurityDescription, g.Symbol, g.Cusip })
.Select(group => new GetRequestTopPositions
{
IssueType = group.First().IssueType,
Symbol = group.First().Symbol,
Cusip = group.First().Cusip,
ClosingPrice = group.First().ClosingPrice,
LatestPrice = group.First().LatestPrice,
ChangeInPrice=group.First().ChangeInPrice,
UnitPrice = group.First().UnitPrice,
PriceFactor = group.First().PriceFactor,
MSDWSecurityCode = group.First().MSDWSecurityCode,
SecurityDescription = group.First().SecurityDescription,
Office = group.First().Office,
Account = group.First().Account,
KeyAccount = group.First().KeyAccount,
Quantity = group.Sum(q => q.Quantity),
MarketValue = group.Sum(q => q.MarketValue),
ChangeInMarketValue = group.Sum(q => q.ChangeInMarketValue),
Count=group.Count()
})
.OrderByDescending(o => o.MarketValue).Take(10);
list_accr = result.ToList<GetRequestTopPositions>();
return list_accr;
}