新列表的 GroupBy bool 和 Select 值
GroupBy bool and Select values for new list
我正在尝试进入更复杂的 Linq 查询并立即抓住我感到卡住的要点。我在数据库中有以下列表:
ID ELAPSED TIME APPISRUNNING
1 12 TRUE
2 54 TRUE
3 32 FALSE
其中 ELAPSED TIME 是 TimeSpan,APPISRUNNING 是布尔值。
我想根据这些值构建图表 (https://github.com/beto-rodriguez/LiveCharts2)。图表构建良好:
Title = "Analytics";
this.ActivityChartSeries = new ISeries[]
{
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
};
现在我不知何故需要首先 GroupBy bool 然后 select 一个新列表?我试过以下:
IEnumerable<DataRecord> dataRecords = await this.DataStore.GetItemsAsync();
this.ActivityChartSeries = dataRecords
.GroupBy(g => g.AppIsRunning)
.Select(m => new
{ // BELOW IS TOTALLY UNCLEAR FOR ME
Values = m.Select(r => r.EngineElapsed.Ticks),
Name = m.Select(r => r.Name),
})
.Select(x =>
new PieSeries<double>
{
Values = new List<double> { x.Values.FirstOrDefault() },
Name = x.Name.FirstOrDefault(),
});
分配变量的类型:
public IEnumerable<ISeries> ActivityChartSeries
这部分我完全不清楚:
Values = m.Select(r => r.EngineElapsed.Ticks),
Name = m.Select(r => r.Name),
GroupBy后如何创建两种类型的数据?基本上我需要
- “应用程序 运行”和“值”
- “应用程序不是 运行”和“值”
编辑:
Somar Zein 提供的代码可以正常编译:
var results = activityChartSeries
.GroupBy(a=> a.AppIsRunning)
.Select(item=> new PieSeries<double>{
Name = item.Key ? "Application is Running" : "Application is not Running",
Values = item.Select(x=> Convert.ToDouble(x.ElapsedTime.Ticks)).ToList()
});
但是结果我得到了这样的东西,为什么它在循环中重新加载?
结果如下:
enter image description here
EDIT2:
所以我创建了一个示例用于测试目的:
Class:
public class DataModel
{
public int Id { get; set; }
public TimeSpan ElapsedTime { get; set; }
public bool AppIsRunning { get; set; }
}
代码:
List<DataModel> records = new List<DataModel>();
records.Add(new DataModel { Id = 1, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 2, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 3, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 4, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 5, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
this.ActivityChartSeries = records
.GroupBy(g => g.AppIsRunning)
.Select(item => new PieSeries<double>
{
Name = item.Key ? "Running" : "Not Running",
Values = new double[] { 2, 4 },
});
我得到了相同的重新加载效果,即使你最初提供的来自 LiveCharts 的示例工作正常。
您可以尝试执行以下操作:
var results = activityChartSeries
.GroupBy(a=> a.AppIsRunning)
.Select(item=> new PieSeries<double>{
Name = item.Key ? "Application is Running" : "Application is not Running",
Values = item.Select(x=> Convert.ToDouble(x.ElapsedTime.Ticks)).ToList()
});
希望对您有所帮助!
我正在尝试进入更复杂的 Linq 查询并立即抓住我感到卡住的要点。我在数据库中有以下列表:
ID ELAPSED TIME APPISRUNNING
1 12 TRUE
2 54 TRUE
3 32 FALSE
其中 ELAPSED TIME 是 TimeSpan,APPISRUNNING 是布尔值。
我想根据这些值构建图表 (https://github.com/beto-rodriguez/LiveCharts2)。图表构建良好:
Title = "Analytics";
this.ActivityChartSeries = new ISeries[]
{
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
new PieSeries<double> { Values = new double[] { 2 }},
};
现在我不知何故需要首先 GroupBy bool 然后 select 一个新列表?我试过以下:
IEnumerable<DataRecord> dataRecords = await this.DataStore.GetItemsAsync();
this.ActivityChartSeries = dataRecords
.GroupBy(g => g.AppIsRunning)
.Select(m => new
{ // BELOW IS TOTALLY UNCLEAR FOR ME
Values = m.Select(r => r.EngineElapsed.Ticks),
Name = m.Select(r => r.Name),
})
.Select(x =>
new PieSeries<double>
{
Values = new List<double> { x.Values.FirstOrDefault() },
Name = x.Name.FirstOrDefault(),
});
分配变量的类型:
public IEnumerable<ISeries> ActivityChartSeries
这部分我完全不清楚:
Values = m.Select(r => r.EngineElapsed.Ticks),
Name = m.Select(r => r.Name),
GroupBy后如何创建两种类型的数据?基本上我需要
- “应用程序 运行”和“值”
- “应用程序不是 运行”和“值”
编辑:
Somar Zein 提供的代码可以正常编译:
var results = activityChartSeries
.GroupBy(a=> a.AppIsRunning)
.Select(item=> new PieSeries<double>{
Name = item.Key ? "Application is Running" : "Application is not Running",
Values = item.Select(x=> Convert.ToDouble(x.ElapsedTime.Ticks)).ToList()
});
但是结果我得到了这样的东西,为什么它在循环中重新加载?
结果如下: enter image description here
EDIT2:
所以我创建了一个示例用于测试目的:
Class:
public class DataModel
{
public int Id { get; set; }
public TimeSpan ElapsedTime { get; set; }
public bool AppIsRunning { get; set; }
}
代码:
List<DataModel> records = new List<DataModel>();
records.Add(new DataModel { Id = 1, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 2, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 3, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 4, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
records.Add(new DataModel { Id = 5, ElapsedTime = new TimeSpan(1, 20, 30), AppIsRunning = true });
this.ActivityChartSeries = records
.GroupBy(g => g.AppIsRunning)
.Select(item => new PieSeries<double>
{
Name = item.Key ? "Running" : "Not Running",
Values = new double[] { 2, 4 },
});
我得到了相同的重新加载效果,即使你最初提供的来自 LiveCharts 的示例工作正常。
您可以尝试执行以下操作:
var results = activityChartSeries
.GroupBy(a=> a.AppIsRunning)
.Select(item=> new PieSeries<double>{
Name = item.Key ? "Application is Running" : "Application is not Running",
Values = item.Select(x=> Convert.ToDouble(x.ElapsedTime.Ticks)).ToList()
});
希望对您有所帮助!