LINQ a ADO.NET ayuda consulta padre / hijo
-
21-09-2019 - |
Pregunta
Quiero usar LINQ para ADO.NET para recuperar todas las filas que coinciden con los criterios siguientes de un DataTable.
- Seleccionar todas las filas donde "parentId" es igual a "id" de una fila donde "parentId" es igual a cero.
- Pedido por "Nombre".
Puede alguien decirme cómo lograr esto (preferiblemente usando tanto la sintaxis de consulta y método de sintaxis), y posiblemente me punto a donde puedo leer más sobre este tema?
Solución
No existe tal cosa como el "LINQ a ADO.NET" (tal vez estás confundiendo con ADO.NET Entity Framework). En su caso, usted parece estar refiriéndose a LINQ a conjuntos de datos
Se podría hacer algo así:
sintaxis de consulta:
var parents = from row in table.AsEnumerable()
where row.IsNull("parentId")
select parents;
var children = from row in table.AsEnumerable()
where parents.Any(p => p.Field<int>("id") = row.Field<int>("parentId"))
orderby row.Field<string>("Name")
select row;
sintaxis Método:
var parents = table.AsEnumerable()
.Where(row => row.IsNull("parentId"));
var children = table.AsEnumerable()
.Where(row => parents.Any(p => p.Field<int>("id") = row.Field<int>("parentId")))
.OrderBy(row => row.Field<string>("Name"));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow