Mostrando las entradas con la etiqueta LINQ. Mostrar todas las entradas

Compare two datatable using LINQ Query

Introduction

Compare two datatable having same datatype column using LINQ Query

Using the code

This tips are used to get Mismatched records from datatable1 compared with datatable2 using LINQ Query.  This mismatched records get from another datatable.
 
var qry1 = datatable1.AsEnumerable().Select(a => new { MobileNo = a["ID"].ToString() });
var qry2 = datatable2.AsEnumerable().Select(b => new { MobileNo = b["ID"].ToString() });
var exceptAB = qry1.Except(qry2);
DataTable dtMisMatch = (from a in datatable1.AsEnumerable()
    join ab in exceptAB on a["ID"].ToString() equals ab.MobileNo
    select a).CopyToDataTable(); 
 
Fuente 
lunes, marzo 12, 2012
Posted by Carlos

WHERE IN con LINQ

 //Si usamos Linq2SQL usar   
 NorthwindDataContext nc = new NorthwindDataContext();   
 NorthwindEntities ne = new NorthwindEntities();   
 var suppliers = from s in ne.Suppliers   
       where (s.CompanyName.StartsWith("E"))   
       select s.SupplierID;   
 int[] supplierIDs = suppliers.ToArray();   
 var products = from p in ne.Products   
       where (supplierIDs.Contains((int)p.SupplierID))   
       select p;   
miércoles, marzo 09, 2011
Posted by Carlos
Tag :

Importar archivos de texto con LINQ

Tenemos un archivo con 3 columnas y un Total que es producto de la segunda por la tercera, el archivo esta delimitado por comas:
Zanahorias, 5,0 .50
Naranjas, 6, 1.25
Manzanas, 7, 2.50
Usamos un método que recorra el archivo y nos devuelva las líneas, en un IEnumerable<String>:
  1: public static IEnumerable<string> ReadLinesFromFile(string filename)
  2: {
  3:     using (StreamReader reader = new StreamReader(filename))
  4:     {
  5:         while (true)
  6:         {
  7:             string s = reader.ReadLine();
  8:             if (s == null)
  9:                 break;
 10:             yield return s;
 11:         }
 12:     }
 13: }

Hacemos la consulta LINQ usando este resultado como fuente de datos:
  1: var products = from line in ReadLinesFromFile(@"c:\import.txt")
  2:                let item = line.Split(’,’)
  3:                select new
  4:                {
  5:                    Producto = item[0],
  6:                    Cantidad = Convert.ToInt32(item[1]),
  7:                    Precio = Convert.ToDecimal(item[2]),
  8:                    Total = Convert.ToInt32(item[1]) * Convert.ToDecimal(item[2])
  9:                };

Y finalmente pasamos el resultado a un DataGrid:

  1: GridView1.DataSource = products;
  2: GridView1.DataBind();
viernes, marzo 04, 2011
Posted by Carlos
Tag :

Populares!

- Copyright © - Oubliette - -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -