Herramientas de usuario

Herramientas del sitio


fw:othersnet:linq

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
fw:othersnet:linq [2009/09/04 07:51]
alfred
fw:othersnet:linq [2020/05/09 09:25] (actual)
Línea 300: Línea 300:
                 .Take(3);                 .Take(3);
 </​code>​ </​code>​
 +
  
 ==== Skip ==== ==== Skip ====
Línea 309: Línea 310:
 <code csharp> <code csharp>
 query.Skip(30).Take(10) query.Skip(30).Take(10)
 +</​code>​
 +
 +
 +
 +===== Otros =====
 +
 +
 +==== let ====
 +La clausula ''​let''​ permite crear una variable dentro del ámbito de la query y utilizarla en la condición. ​
 +
 +<code csharp>
 +var lArrivals = from a in db.PIU_ARRIVALS
 +                let t = (DateTime.Now - a.updatedLocalTime)
 +                where (t.Seconds > seconds)
 +                select a;
 +</​code>​
 +... O ...
 +<code csharp>
 +from car in root.Elements("​car"​)
 +let profiles =
 +  from profile in car.Elements("​profile"​)
 +  select new {
 +    Name = profile.Attribute("​name"​).Value,​
 +    Value = profile.Attribute("​value"​).Value
 +  }
 +let supports =
 +  from support in car.Elements("​support"​)
 +  select new {
 +    Name = support.Attribute("​name"​).Value,​
 +    Value = support.Attribute("​value"​).Value
 +  }
 +select new Car {
 +  Name = car.Attribute("​name"​).Value,​
 +  Vendor = profiles.Single(prof => prof.Name == "​Vendor"​).Value,​
 +  Model = profiles.Single(prof => prof.Name == "​Model"​).Value,​
 +  Doors = int.Parse(profiles.Single(prof => prof.Name == "​Doors"​).Value),​
 +  RacingSupport = supports.Single(sup => sup.Name == "​Racing"​).Value == "​yes"​
 +};
 </​code>​ </​code>​
  
Línea 365: Línea 404:
         where (p.IIDPARADA == id)         where (p.IIDPARADA == id)
         select p).ToList();​         select p).ToList();​
 +</​code>​
 +=== Ejemplo 3 (NOT IN) ===
 +<​code>​
 +var query =
 +    from c in dc.Customers
 +    where !(from o in dc.Orders
 +            select o.CustomerID)
 +           ​.Contains(c.CustomerID)
 +    select c;
 +foreach (var c in query) Console.WriteLine( c );
 +</​code>​
 +=== Ejemplo 4 (FindAll y ForEach) ===
 +<​code>​
 +static void DisplayInstalledApplications2()
 +{
 +  string registryKey =
 +    @"​SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";​
 +
 +  using (Microsoft.Win32.RegistryKey key = 
 +    Registry.LocalMachine.OpenSubKey(registryKey))
 +  {
 +    (from a in key.GetSubKeyNames()
 +      let r = key.OpenSubKey(a)
 +      select new
 +      {
 +        Application = r.GetValue("​DisplayName"​)
 +      }).ToList()
 +      .FindAll(c => c.Application != null)
 +      .ForEach(c => Console.WriteLine(c.Application));​
 +  }
 +}
 +</​code>​
 +=== Cálculo del tamaño de un directorio ===
 +<code csharp>
 +static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir) {
 +  // Enumerate all the files
 +  long totalSize = dInfo.EnumerateFiles()
 +    .Sum(file => file.Length);​
 +  // If Subdirectories are to be included
 +  if (includeSubDir)
 +  {
 +    // Enumerate all sub-directories
 +    totalSize += dInfo.EnumerateDirectories()
 +      .Sum(dir => DirectorySize(dir,​ true));
 +  }
 +  return totalSize;
 +}
 </​code>​ </​code>​
  
fw/othersnet/linq.1252050664.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)