System.Linq.Dynamic.Core icon indicating copy to clipboard operation
System.Linq.Dynamic.Core copied to clipboard

Select Nested with Includes

Open ignaciosalazarcalle opened this issue 1 year ago • 1 comments

Hello,

I'm trying resolve this query with DynamicLinq:

select u.nombre
from expediente e
inner join clave_principal cp on cp.id = e.clave_principal_id
inner join area_unidad au on au.id = cp.area_unidad_id
inner join unidad u on u.id = au.unidad_id
where e.id = 38269

I have a datatable with 4 fields:

  • TableName: Expediente
  • Select: ClavePrincipal.AreaUnidad.Unidad.Nombre as NombreUnidad???
  • Where: Id == @0
  • Include: ClavePrincipal.AreaUnidad.Unidad

And I want get the value the select field dynamically. I construct a Queryable<T> from TableName and I transform these fields:

if (!string.IsNullOrWhiteSpace(parametroPlantilla.Where))
{
    listQueryableEntity = listQueryableEntity
        .Where(parametroPlantilla.Where, request.ExpedienteId);
}

if (!string.IsNullOrWhiteSpace(parametroPlantilla.Include))
{
    listQueryableEntity = listQueryableEntity
        .Include(parametroPlantilla.Include);
}

List<dynamic> listEntity = Enumerable.Empty<dynamic>().ToList();
if (!string.IsNullOrWhiteSpace(parametroPlantilla.Select))
{
    ParsingConfig parsingConfig = new ParsingConfig
    {
        NullPropagatingUseDefaultValueForNonNullableValueTypes = true,
        PrioritizePropertyOrFieldOverTheType = false
    };
    listEntity = await listQueryableEntity
        .Select(parsingConfig, parametroPlantilla.Select)
        .ToDynamicListAsync(cancellationToken: cancellationToken);
}

My classes:

public class Expediente
{
	public int Id {get;set;}
	public string Nombre {get;set;}
	public int ClavePrincipalId {get;set;}
	public ClavePrincipal ClavePrincipal {get;set;}
}

public class ClavePrincipal
{
	public int Id {get;set;}
	public string Nombre {get;set;}
	public int AreaUnidadId {get;set;}
	public AreaUnidad AreaUnidad {get;set;}
}

public class AreaUnidad
{
	public int Id {get;set;}
	public string Nombre {get;set;}
	public int UnidadId {get;set;}
	public Unidad Unidad {get;set;}
}

public class Unidad
{
	public int Id {get;set;}
	public string Nombre {get;set;}
}

I'm using .Net 6 ant the last System.Linq.Dynamic.Core = 1.3

Regards,

ignaciosalazarcalle avatar Mar 04 '23 13:03 ignaciosalazarcalle

I think I have a similar issue:

Classes:

<Table("appointment")>
Public Class appointment
    <ComponentModel.DataAnnotations.Key>
    <Column("id", Order:=0)>
    Public Property id As Integer

    <Column("descriptionid")>
    Public Property descriptionid As Integer?

    <ForeignKey("descriptionid")>
    Public Property description As description
End Class

<Table("description")>
Public Class description
    <ComponentModel.DataAnnotations.Key>
    <Column("id", Order:=0)>
    Public Property id As Integer

    <Column("text")>
    Public Property text As String
End Class

Query: dbContext.appointments.Include("description").Where(...).OrderBy(...).Select("new(id, description.text)")

Exception: System.Linq.Dynamic.Core.Exceptions.ParseException: "No property or field 'text' exists in type 'appointment'"

So even though I included the ForeignKey property, Dynamic LINQ does not understand that text is a property of description and not appointment.

Hurricane31337 avatar Jun 13 '23 17:06 Hurricane31337