Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
fw:othersnet:linq [2010/03/02 13:20] alfred |
fw:othersnet:linq [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 315: | Línea 315: | ||
| ===== Otros ===== | ===== Otros ===== | ||
| + | |||
| + | |||
| ==== let ==== | ==== 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> | ||
| + | |||
| + | |||
| Línea 369: | 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> | ||