[英]c# LINQ LIST Key, Results view, get values
I'm starting to use C # and LINQ, I think a list to filter a database that has repeated data by date and created a List, through ToList (). 我开始使用C#和LINQ,我认为一个列表来过滤一个按日期重复数据并通过ToList()创建List的数据库。
List<Registro> datosCalcA = new List<Registro>();
MonitoreoEntities _context = new MonitoreoEntities();
datosCalcA= _context.Registroes.Where(a => a.PuntoDeMedicionId == pPuntoDeMedicionId
&& a.EntradaSalidaId == _operadorA
|| a.EntradaSalidaId == _operadorB
&& a.FechaHoraRegistro <= _fechaHoraFinal
&& a.FechaHoraRegistro >= _fechaHoraInicial).OrderBy(i => i.FechaHoraRegistro).ToList();
var newList = datosCalcA.GroupBy(i => i.FechaHoraRegistro, x => new {Valor = x.Valor}).ToList();
I want to access the values that I have in the list. 我想访问列表中的值。
If I use a breackpoint, I can see: 如果我使用breackpoint,我可以看到:
- newList[0] {System.Linq.Lookup<System.DateTime,<>f__AnonymousType0<decimal>>.Grouping} System.Linq.IGrouping<System.DateTime,<>f__AnonymousType0<decimal>> {System.Linq.Lookup<System.DateTime,<>f__AnonymousType0<decimal>>.Grouping}
+ [System.Linq.Lookup<System.DateTime,<>f__AnonymousType0<decimal>>.Grouping] {System.Linq.Lookup<System.DateTime,<>f__AnonymousType0<decimal>>.Grouping} System.Linq.Lookup<System.DateTime,<>f__AnonymousType0<decimal>>.Grouping
+ Key {05/12/2014 01:52:07 p.m.} System.DateTime
- Results View Expanding the Results View will enumerate the IEnumerable
+ [0] { Valor = 29 } <Anonymous Type>
+ [1] { Valor = 24 } <Anonymous Type>
I have read the values 0 and 1. 我已经读过值0和1。
I need to make the product between the two values and generate a new list with the result, the serious code as: 我需要在两个值之间生成产品并生成一个包含结果的新列表,严重的代码如下:
var newList = datosCalcA.GroupBy(i => i.FechaHoraRegistro, x => new {Valor = x.Valor}).ToList();
for (int i = 0; i < newList.Count; i++) // Loop with for.
{
var datos = newList[i].First().Valor[0,i]*newList[i].First().Valor[1,i];
}
With GroupBy, I group the data by date, and then I have to calculate a date for the products of the two data are in. 使用GroupBy,我按日期对数据进行分组,然后我必须计算两个数据的产品的日期。
Before grouping I have: 在分组之前我有:
date1 Valor=29, ID1 date1 Valor = 29,ID1
date1 Valor=24, ID2 date1 Valor = 24,ID2
date2 Valor=24, ID1 date2 Valor = 24,ID1
date2 Valor=23, ID2 date2 Valor = 23,ID2
After grouping, I lose IDxx, this is not any problem 分组后,我失去了IDxx,这不是问题
date1 Valor=29, Valor=24 date1 Valor = 29,Valor = 24
date2 Valor=25, Valor=23 date2 Valor = 25,Valor = 23
With the data grouped by date 数据按日期分组
I need to calculate where value is the same variable, they are different fields within the record 我需要计算值是同一个变量的位置,它们是记录中的不同字段
+[0] { Valor = 29 } <Anonymous Type>
+[1] { Valor = 24 } <Anonymous Type>
date1 Valor * Valor (29*24) date1 Valor * Valor(29 * 24)
date2 Valor * Valor (24*23) date2 Valor * Valor(24 * 23)
Not if the code is correct, is one of the ways I thought it could solve. 如果代码是正确的,那就不是我认为可以解决的方法之一。
I could solve. 我可以解决。
As is a list inside another list, I create a auxiliary list each major item, I take the values they have in each: 作为另一个列表中的列表,我在每个主要项目中创建一个辅助列表,我将每个项目中的值都包含在内:
var newList = datosCalcA.GroupBy(i => i.FechaHoraRegistro, x => new {x.Valor}).ToList();
yy = 0;
foreach (var _datosCalculado in newList)
{
var valorW = newList[yy].ToList(); //auxiliary list to take values (Valor)
try
{
decimal resultado = valorW[0].Valor / valorW[1].Valor;// Take data in pos [0] and [1] in auxiliary list
}
catch (DivideByZeroException e) //divide by zero error
{
decimal resultado = 0;
}
yy++;
}
Assume your class looks like this: 假设您的课程如下:
public class myClass
{
public int Valor1;
public int Valor2;
}
Now you make a list of myClass somehow, perhaps from getting data from db: 现在你以某种方式列出myClass,也许是从db获取数据:
List<myClass> myList = GetDataFromDatabase(); // Returns List<myClass>
Now you can make a new anonymous type or you can select into a different type like this 现在您可以创建一个新的匿名类型,或者您可以选择这样的其他类型
var stuff = myList.Select(x => new { Product = x.Valor1 * x.Valor2 });
You can use this code 您可以使用此代码
var ListProduct = from item in myList
select (item.Valor2 * item.Valor1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.