c# set entity properties from another key value entity - c#

I have an entity which contains key and values. My key is enum. Values are string on this entity (but their data type may differ from string).
Enum example:
public enum CallKey {
CallDate = 1,
CallTime = 2,
FromPhoneNumber = 3,
ToPhoneNumber = 4,
Duration = 5,
FromOperatorCode = 6,
ToOperatorCode = 7
}
My key value entity is:
public class CallKeyValue {
public CallKey CallKey { get; set; }
public string Value1 { get; set; }
public string Value2 { get; set; }
}
My example key, value data is:
CallKey |Value1 |Value2
1 |11.04.2017 |
2 |15:43 |
3 |5311234567 |
4 |5311234587 |
5 |13*min |
6 |TR |001
7 |TR |002
Now, I want to create my final entity from my key value entity.
My final entity:
public class CallDetail{
public DateTime CallDate { get; set; } //=15.04.2017 15:43
public string FromPhoneNumber { get; set; } //=5311234567
public string ToPhoneNumber { get; set; } //=5311234587
public int Duration { get; set; } //=13
public DurationUnit DurationUnit { get; set; } //=1 (this is enum 1:min, 2:hour etc...)
public string FromOperatorCountry { get; set; } //=TR
public string FromOperatorId { get; set; } //=001
public string ToOperatorCountry { get; set; } //=TR
public string ToOperatorId { get; set; } //=002
}
Which way to set CallDetail entity? Reflection, property by property or another better way?

Ok, I think I found some solutions about this problem. I did 3 tests and I add here a simple sample. I think second is best for me, but I decide to do a loading test.
public class CallTest {
public void TestCode() {
var items = new List<CallKeyValue>
{
new CallKeyValue {CallKey = CallKey.CallDate, Value1 = "11.04.2017"},
new CallKeyValue {CallKey = CallKey.CallTime, Value1 = "15:43"},
new CallKeyValue {CallKey = CallKey.FromPhoneNumber, Value1 = "5311234567"},
new CallKeyValue {CallKey = CallKey.ToPhoneNumber, Value1 = "5311234587 "},
new CallKeyValue {CallKey = CallKey.Duration, Value1 = "13*min"},
new CallKeyValue {CallKey = CallKey.FromOperatorCode, Value1 = "TR",Value2 = "001"},
new CallKeyValue {CallKey = CallKey.ToOperatorCode, Value1 = "TR",Value2 = "002"},
};
var st = new Stopwatch();
//Test 1: ElapsedMilliseconds: 50, ElapsedTicks: 122023
st.Start();
IDictionary<string,dynamic> expando = new ExpandoObject();
foreach(var item in items) {
expando.Add(item.CallKey.ToString(),null);
expando[item.CallKey.ToString()] = new { Value1 = item.Value1,Value2 = item.Value2 };
}
var cd = new CallDetail {
CallDate = DateTime.Parse(expando[CallKey.CallDate.ToString()].Value1 + " " + expando[CallKey.CallTime.ToString()].Value1),
FromPhoneNumber = expando[CallKey.FromPhoneNumber.ToString()].Value1,
ToPhoneNumber = expando[CallKey.ToPhoneNumber.ToString()].Value1,
Duration = Convert.ToInt32(expando[CallKey.Duration.ToString()].Value1.ToString().Split('*')[0]),
DurationUnit = GetEnumKeyOfUnit(expando[CallKey.Duration.ToString()].Value1.ToString().Split('*')[1]),
FromOperatorCountry = expando[CallKey.FromOperatorCode.ToString()].Value1,
FromOperatorId = expando[CallKey.FromOperatorCode.ToString()].Value2,
ToOperatorCountry = expando[CallKey.ToOperatorCode.ToString()].Value1,
ToOperatorId = expando[CallKey.ToOperatorCode.ToString()].Value2
};
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds);
Console.WriteLine(st.ElapsedTicks);
st.Reset();
//Test 2: ElapsedMilliseconds: 0, ElapsedTicks: 328
st.Start();
var cd3 = items.Select(s => {
IDictionary<string,dynamic> expando2 = new ExpandoObject();
expando2.Add(s.CallKey.ToString(),null);
expando2[s.CallKey.ToString()] = new { Value1 = s.Value1,Value2 = s.Value2 };
var item = new CallDetail {
CallDate = DateTime.Parse(expando2[CallKey.CallDate.ToString()].Value1 + " " + expando2[CallKey.CallTime.ToString()].Value1),
FromPhoneNumber = expando2[CallKey.FromPhoneNumber.ToString()].Value1,
ToPhoneNumber = expando2[CallKey.ToPhoneNumber.ToString()].Value1,
Duration = Convert.ToInt32(expando2[CallKey.Duration.ToString()].Value1.ToString().Split('*')[0]),
DurationUnit = GetEnumKeyOfUnit(expando2[CallKey.Duration.ToString()].Value1.ToString().Split('*')[1]),
FromOperatorCountry = expando2[CallKey.FromOperatorCode.ToString()].Value1,
FromOperatorId = expando2[CallKey.FromOperatorCode.ToString()].Value2,
ToOperatorCountry = expando2[CallKey.ToOperatorCode.ToString()].Value1,
ToOperatorId = expando2[CallKey.ToOperatorCode.ToString()].Value2
};
return item;
});
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds);
Console.WriteLine(st.ElapsedTicks);
st.Reset();
//Test 3: ElapsedMilliseconds: 0, ElapsedTicks: 1390
st.Start();
var cd2 = new CallDetail {
CallDate = DateTime.Parse(items.FirstOrDefault(f => f.CallKey == CallKey.CallDate)?.Value1 + " " + items.FirstOrDefault(f => f.CallKey == CallKey.CallTime)?.Value1),
FromPhoneNumber = items.FirstOrDefault(f => f.CallKey == CallKey.FromPhoneNumber)?.Value1,
ToPhoneNumber = items.FirstOrDefault(f => f.CallKey == CallKey.ToPhoneNumber)?.Value1,
Duration = Convert.ToInt32(items.FirstOrDefault(f => f.CallKey == CallKey.Duration)?.Value1.Split('*')[0]),
DurationUnit = GetEnumKeyOfUnit(items.FirstOrDefault(f => f.CallKey == CallKey.Duration)?.Value1.Split('*')[1]),
FromOperatorCountry = items.FirstOrDefault(f => f.CallKey == CallKey.FromOperatorCode)?.Value1,
FromOperatorId = items.FirstOrDefault(f => f.CallKey == CallKey.FromOperatorCode)?.Value2,
ToOperatorCountry = items.FirstOrDefault(f => f.CallKey == CallKey.ToOperatorCode)?.Value1,
ToOperatorId = items.FirstOrDefault(f => f.CallKey == CallKey.ToOperatorCode)?.Value2
};
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds);
Console.WriteLine(st.ElapsedTicks);
}
private DurationUnit? GetEnumKeyOfUnit(string unit) {
switch(unit) {
case "min":
return DurationUnit.Minute;
case "hour":
return DurationUnit.Hour;
}
return null;
}
}
Before this test, I try to use reflection. Reflection performance is very bad than 3 tests. It is elapsedmilliseconds is about 100, also elapsedticks is very high. I may use second test until find to better way.

Related

How to aggregate data, pass and fail as final result?

I have a list of result either having or not having Data plus has Pass and Fail like below,
var results = new List<Result>
{
new Result{Data = new Data{Name = "A"}, Pass = 1, Fail = 0},
new Result{Data = new Data{Name = "B"}, Pass = 3, Fail = 1},
new Result{Pass = 1, Fail = 0}
};
I need to aggregate data and need this as final result output,
var finalResult = new FinalResult
{
Datas = new List<Data> { new Data { Name = "A" }, new Data { Name = "B" } },
TotalPass = 5,
TotalFail = 1,
Divident = 5/1
}
I tried something like below, but totalPass and totalfail, are not coming correct. Plus, how to aggregate Data?
int totalPass = 0;
int totalfail = 0;
var finalResult = new FinalResult();
foreach (var r in results)
{
totalPass += r.Pass;
totalfail += r.Fail;
}
finalResult.TotalFail = totalPass;
finalResult.TotalFail = totalfail;
finalResult.Divident = totalPass / totalfail;
Here are the two classes:
public class FinalResult
{
public List<Data> Datas { get; set; }
public int TotalPass { get; set; }
public int TotalFail { get; set; }
public int Divident { get; set; }
}
public class Result
{
public Data Data { get; set; }
public int Pass { get; set; }
public int Fail { get; set; }
}
public class Data
{
public string Name { get; set; }
}
You can achieve this easily using LINQ:
var finalResult = new FinalResult
{
Datas = results.Where(r => r.Data != null).Select(r => r.Data).ToList(),
TotalPass = results.Sum(r => r.Pass),
TotalFail = results.Sum(r => r.Fail)
};
// Beware of division by zero and integer division.
finalResult.Divident = finalResult.TotalPass / finalResult.TotalFail;
Notes:
You should probably check the value of TotalFail before the division to prevent division by zero.
As Groo mentioned in the comments, Divident should probably be declared as double and you should cast one of the parts to double unless you do want Integer Division.

need to group data based on `InstanceData` Name property

I have one Packet like below,
var dataPacket = new Packet
{
Id = new Guid("2e08bd98-68eb-4358-8efb-9f2adedfb034"),
Results = new Result
{
ResultName = "ResultName1",
Instances = new List<Instance>
{
new Instance
{
InstanceName = "InstanceName1",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V1"},
new InstanceData{Name = "N2", Value = "V2"}
}
},
new Instance
{
InstanceName = "InstanceName2",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V3"},
new InstanceData{Name = "N2", Value = "V4"}
}
}
}
}
};
Here are the class structures,
public class Packet
{
public Guid Id { get; set; }
public Result Results { get; set; }
}
public class Result
{
public string ResultName { get; set; }
public List<Instance> Instances { get; set; }
}
public class Instance
{
public string InstanceName { get; set; }
public List<InstanceData> InstanceDatas { get; set; }
}
public class InstanceData
{
public string Name { get; set; }
public string Value { get; set; }
}
For above Packet I want to spilt this into 2 Packets based on InstanceData common Name
All N1 from InstanceName1 and InstanceName2 into one packet
All N2 from InstanceName1 and InstanceName2 into one packet
Packet1 should be like this,
var packet1 = new Packet
{
Id = new Guid("2e08bd98-68eb-4358-8efb-9f2adedfb034"),
Results = new Result
{
ResultName = "ResultName1",
Instances = new List<Instance>
{
new Instance
{
InstanceName = "InstanceName1",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V1"},
}
},
new Instance
{
InstanceName = "InstanceName2",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V3"},
}
}
}
}
};
and similarly packet2.
I have tried below, but this will split on InstanceData as well and giving 4 packets.
var packets = dataPacket.Results
.Instances
.SelectMany(x =>
x.InstanceDatas.Select(y => new Packet()
{
Id = dataPacket.Id,
Results = new Result()
{
ResultName = dataPacket.Results.ResultName,
Instances = new List<Instance>()
{
new Instance()
{
InstanceDatas = new List<InstanceData>() {y},
InstanceName = x.InstanceName
}
}
}
}));
You can write a helper method which finds the possible names as keys and iterate over the keys. Then you build new object instances for each key you are checking. The source code can look like this:
private static IList<Packet> SplitByName(Packet packet) {
IList<string> names = packet.Results.Instances
.SelectMany(it => it.InstanceDatas)
.Select(it => it.Name)
.Distinct()
.ToList();
IList<Packet> result = new List<Packet>();
foreach (string name in names)
{
List<Instance> newInstances = packet.Results.Instances
.Select(it => new Instance {
InstanceName = it.InstanceName,
InstanceDatas = it.InstanceDatas
.Where(it => it.Name == name)
.ToList()
})
.Where(it => it.InstanceDatas.Any())
.ToList();
Result newResult = new Result {
ResultName = packet.Results.ResultName,
Instances = newInstances
};
result.Add(new Packet {
Id = packet.Id,
Results = newResult
});
}
return result;
}
For each name you are filtering the InstanceData instances for each Instance object. Depending on your needs you might want to add .Where(it => it.InstanceData.Any()) so you don't have any "empty" instances.

How to convert list item to multidimensional array in c#?

I have a single list of data like
List 0 index Contains : BlockId = 438001,DistrictId = 438,TownId = 0,UserId = 1077
UserTypeId = 4,VillageId = 238047,ZoneId = 1018
List 1 index contains : BlockId = 438001,DistrictId = 438,TownId = 0,UserId = 1077,UserTypeId = 4,VillageId = 238048,ZoneId = 1018
List 2 index contains : BlockId = 438002,DistrictId = 438,TownId = 0,UserId = 1077,UserTypeId = 4,VillageId = 238138,ZoneId = 1018
And now I want to create a multidimensional array according to that like
public List<int>[][] arrayList{get;set;}
I need to convert list data to multidimensional array list like
[0]
[0] -> Count 1 -> 438001 --> Showing blockId
[1]-> Count 3 -> 238047,238048 --> showing villageId
[1]
[0]->count 1 -> 438002
[1]->count 2 -> 238138
[2]->count 3-> 0 --> showing townId
var data = user.getUserRolebyUserId(userId);
var blkList = data.GroupBy(x => x.BlockId);
List<int>[][] lst;
foreach (var item in data.GroupBy(x => x.BlockId))
{
List<int>[][] array = [item.Key,item.Select(x => x.VillageId).ToList(), item.Select(q => q.TownId)];
}
I am trying to applying this but it's showing me error for Invalid Expression.
So How can I convert list into multidimensional array ?
Use a Dictionary :
class Program
{
static void Main(string[] args)
{
List<Location> locations = new List<Location>() {
new Location() { BlockId = 438001,DistrictId = 438,TownId = 0,UserId = 1077, UserTypeId = 4,VillageId = 238047,ZoneId = 1018 },
new Location() { BlockId = 438001,DistrictId = 438,TownId = 0,UserId = 1077,UserTypeId = 4,VillageId = 238048,ZoneId = 1018},
new Location() { BlockId = 438002,DistrictId = 438,TownId = 0,UserId = 1077,UserTypeId = 4,VillageId = 238138,ZoneId = 1018 }
};
Dictionary<int, List<int>> dict = locations
.GroupBy(x => x.BlockId, y => y.Items)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
public class Location
{
public int BlockId { get; set; }
public int DistrictId { get; set; }
public int TownId { get; set; }
public int UserId { get; set; }
public int UserTypeId { get; set; }
public int VillageId { get; set; }
public int ZoneId { get; set; }
public List<int> Items {
get { return new List<int> { BlockId, DistrictId, TownId, UserId, UserTypeId, VillageId, ZoneId }; }
set {;}
}
}

Cannot pass KeyValuePair to controller, values null

im trying pass a object since View to controller as ViewModel. But some arrive field empty to controller. This fields are KeyValuePair and have not idea for pass it correctly to Controller.
This is my code:
Controller:
public ActionResult Index(SearchResultsViewModel searchResultsViewModel)
{
PassengerDataViewModel viewModel = new PassengerDataViewModel()
{
Request = searchResultsViewModel,
PassengerType = GetPassengerTypes(),
NumberPeople = GetNumberPeople()
};
return View("PassengerData", viewModel);
}
ViewModel:
public class SearchResultsViewModel
{
public SearchViewModel Request { get; set; }
public string SearchFlightDatesText { get; set; }
public string SearchPaxSelectionText { get; set; }
public KeyValuePair<string, string> DepartureStation { get; set; }
public KeyValuePair<string, string> ArrivalStation { get; set; }
public DateTime BeginDate { get; set; }
public DateTime EndDate { get; set; }
public DateTime SelectedDate { get; set; }
public int SelectedDay { get; set; }
public List<FlightsResponseDTO> Days { get; set; }
public KeyValuePair<string, string> DepartureStation2 { get; set; }
public KeyValuePair<string, string> ArrivalStation2 { get; set; }
public List<FlightsResponseDTO> Days2 { get; set; }
public DateTime BeginDate2 { get; set; }
public DateTime EndDate2 { get; set; }
public DateTime SelectedDate2 { get; set; }
public int SelectedDay2 { get; set; }
}
View Code
Post = function (path, params, method) {
method = method || "post"; // Set method to post by default if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
debugger;
for (var key in params) {
if (params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
debugger;
document.body.appendChild(form);
form.submit();
}
$("a.baseButton.yellowButton.mediumButton").click(function (event) {
//Comprobamos si esta seleccionado un vuelo
if(!checkIfFlightSelected()){
$('#error-dialog').dialog({
modal: true,
height: 130,
width: 360
});
event.preventDefault();
return false;
}
//--------------Inicio SearchViewModel ------------------------------
debugger;
var departureStation = "#Model.Request.SelectedOrigin1";
var arrivalStation = "#Model.Request.SelectedDestination1";
var departureDate = "#Model.Request.SelectedFlightDate1";
var departureStation2 = "#Model.Request.SelectedOrigin2";
var arrivalStation2 = "#Model.Request.SelectedDestination2";
var departureDate2 = "#Model.Request.SelectedFlightDate2";
var adults = "#Model.Request.NumAdults";
var childs = "#Model.Request.NumChilds";
var infants = "#Model.Request.NumInfants";
var residentOrFamily ="#Model.Request.SelectedResidentOrLargueFamilyDiscount";
var PassengersWithDiscount = "#Model.Request.NumPassengersWithSpecialDiscount";
var tipo = "#Model.Request.SelectedSearchType";
var searchViewModelRequest = {
SelectedSearchType: tipo,
SelectedOrigin1: departureStation,
SelectedDestination1: arrivalStation,
SelectedFlightDate1: departureDate,
Tipo:tipo,
//SelectedOrigin2: departureStation2,
//SelectedDestination2: arrivalStation2,
//SelectedFlightDate2: departureDate2,
NumAdults: adults,
NumChilds: childs,
NumInfants: infants,
SelectedResidentOrLargueFamilyDiscount: residentOrFamily,
NumPassengersWithSpecialDiscount: PassengersWithDiscount
};
//-------------------Fin SearchViewModel---------------------------------
//Inicio SearchResultsViewModel
//-----------Datos para obtenr datos seleccioandos------------------
var partsId = $("#availabilityTable0 :input:checked")[0].id.split('_');
var day = calendariCarregat.Days[partsId[1]];
var journey=day.Journeys[partsId[2]];
var departureDate = new Date(journey.JourneySellKey.split('~')[5].split(' ')[0]);
//var arrivalDate = new Date(journey.JourneySellKey.split('~')[7].split(' ')[0]);
var departureHourDate = journey.DepartureTimeString;
var arrivalHourDate = journey.ArrivalTimeString;
var departureDay;
var departureMonth;
if (departureDate.getUTCDate() + 1 < 10) {
departureDay = "0" + (departureDate.getUTCDate() + 1);
} else {
departureDay = (departureDate.getUTCDate() + 1);
}
if (departureDate.getUTCDate() + 1 < 10) {
departureMonth = "0" + (departureDate.getUTCMonth() + 1);
} else {
departureMonth = (departureDate.getUTCMonth() + 1);
}
var normalDepartureDate = (departureDay) + "/" + (departureMonth) + "/" + departureDate.getUTCFullYear();
//----------------------Fin datos seleccionados--------------
var searchFlightDatesText = "#Model.SearchFlightDatesText";
var searchPaxSelectionText = "#Model.SearchPaxSelectionText";
var departureStation = {
"Key": "#Model.DepartureStation.Key",
"Value": "#Model.DepartureStation.Value"
};
var arrivalStation = {
"Key": "#Model.ArrivalStation.Key",
"Value": "#Model.ArrivalStation.Value"
};
var beginDate = "#Model.BeginDate";
var endDate = "#Model.EndDate";
var selectedDate = normalDepartureDate;
var selectedDay = "#Model.SelectedDay";
var days = "#Model.Days";
//var departureStation2 = null;
//var arrivalStation2 = null;
//var days2 = null;
//var beginDate2 = null;
//var endDate2 = null;
//var selectedDate2 = null;
//var selectedDay2 = null;
var searchResultsViewModel = {
//Request: searchViewModelRequest.toString(),
"Request.SelectedOrigin1": searchViewModelRequest.SelectedOrigin1,
"Request.SelectedSearchType": searchViewModelRequest.Tipo,
"Request.SelectedDestination1": searchViewModelRequest.SelectedDestination1,
"Request.SelectedFlightDate1": searchViewModelRequest.SelectedFlightDate1,
"Request.NumAdults": searchViewModelRequest.NumAdults,
"Request.NumChilds": searchViewModelRequest.NumChilds,
"Request.NumInfants": searchViewModelRequest.NumInfants,
"Request.IncrementSelectedFlightDate1": 0,
"Request.SelectedResidentOrLargueFamilyDiscount": searchViewModelRequest.SelectedResidentOrLargueFamilyDiscount,
"Request.NumPassengersWithSpecialDiscount": searchViewModelRequest.NumPassengersWithSpecialDiscount,
SearchFlightDatesText: searchFlightDatesText,
SearchPaxSelectionText: searchPaxSelectionText,
"DepartureStation.Key": "test",
"DepartureStation.Value": "test",
"ArrivalStation": "{ [test, test]}",
BeginDate: beginDate,
EndDate: endDate,
SelectedDate: selectedDate,
SelectedDay: selectedDay,
Days: days,
"DepartureStation2.Key": "test",
"DepartureStation2.Value": "test",
};
//--------------Fin SearchResultsViewModel
MICESearcher.Post('#Url.Action("Index", "PassengerData")', (searchResultsViewModel));
});
i have tried it pass values of many ways to the KeyValuePair but i could not achieve it
Edit(solved):
finally thanks to #Daniel J.G, I could solve the problem by following this website ASP MVC.NET - how to bind KeyValuePair?
thank you all for the help!!

Select Max value from sublist with LINQ

How to write a LINQ query that would return row of type "Value" with Max Date and Max value for that date. it should be the row where name = "Correct"
I've written a query at the end, and it working, just trying to find a correct way of doing this.
Thanks in advance
public class MeasurePoint
{
public int No { get; set; }
public string Weight { get; set; }
public List<Values> Vals { get; set; }
}
public class Values
{
public string Name { get; set; }
public int Val { get; set; }
public DateTime Date { get; set; }
}
public static class Test
{
public static void Calc()
{
var mps = new List<MeasurePoint>();
mps.Add(new MeasurePoint()
{
No = 1,
Vals = new List<Values>()
{
new Values(){Date = DateTime.Now.Date.AddDays(1), Name = "testas", Val = 1},
new Values(){Date = DateTime.Now.Date.AddDays(2), Name = "testas", Val = 5},
new Values(){Date = DateTime.Now.Date, Name = "testas", Val = 15}
}});
mps.Add(new MeasurePoint()
{
No = 2,
Vals = new List<Values>()
{
new Values(){Date = DateTime.Now.Date, Name = "testas", Val = 11},
new Values(){Date = DateTime.Now.Date.AddDays(2), Name = "Correct", Val = 55},
new Values(){Date = DateTime.Now.Date, Name = "testas", Val = 15}
}
});
mps.Add(new MeasurePoint()
{
No = 3,
Vals = new List<Values>()
{
new Values(){Date = DateTime.Now.Date.AddDays(1), Name = "testas", Val = 111},
new Values(){Date = DateTime.Now.Date.AddDays(2), Name = "testas", Val = 52},
new Values(){Date = DateTime.Now.Date, Name = "testas", Val = 15}
}
});
mps.Add(new MeasurePoint()
{
No = 4,
Vals = new List<Values>()
});
var x = mps.ElementAt(0).Vals.Union(mps.ElementAt(1).Vals).Union(mps.ElementAt(2).Vals);
var z = x.Where(p => p.Date == x.Max(d => d.Date)).MaxBy(t=>t.Val);
//One more way I've found
var ttt = mps.SelectMany(p => p.Vals).GroupBy(t=>t.Date).MaxBy(r=>r.Key).MaxBy(g=>g.Val);
}
var max = mps.SelectMany(x => x.Vals)
.Aggregate((a, x) => (x.Date > a.Date) ||
((x.Date == a.Date) && (x.Val > a.Val)) ? x : a);
try this
var result = mps.Where(m => m.Vals.Count > 0)
.SelectMany(m => m.Vals
.OrderByDescending(v => v.Date)
.Take(1), (m, v) => new {m.No, v.Date, v.Name, v.Val});
EDIT - this is a new version as issue has become more clear
var result = mps.Where(m => m.Vals.Count > 0)
.SelectMany(m => m.Vals)
.OrderByDescending(v => v.Date)
.ThenByDescending(v => v.Val).Take(1);
How can use call MaxBy() if it's a method of IObservable<T> meanwhile GroupBy() returns IEnumerable<T> ?

Categories

Resources