简体   繁体   中英

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 ().

 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:

-       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] { Valor = 29 }
  • [1] { Valor = 24 }

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.

Before grouping I have:

date1 Valor=29, ID1

date1 Valor=24, ID2

date2 Valor=24, ID1

date2 Valor=23, ID2

After grouping, I lose IDxx, this is not any problem

date1 Valor=29, Valor=24

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)

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:

  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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM