Convert Entity with navigation property to DTO using IQueryable

女生的网名这么多〃 提交于 2019-12-12 00:38:49

问题


Suppose I have following entities and dtos

public class Country
{
    public List<NameLocalized> NamesLocalized;
    public CountryData Data;
}

public class NameLocalized
{
    public string Locale;
    public string Value;
}

public class CountryData
{
    public int Population;
}

public class CountryDto
{
    public String Name;
    public CountryDataDto Data;
}

public class CountryDataDto
{
    public int Population;
}

I need to convert Country to CountryDto (and ideally, I want to make a single query to the database). I have received few suggestions in my other questions on Stackoverflow, and can now accomplish the task, but only partially. I am stuck at how to convert navigation property (CountryData in this case). I was suggested to use LINQKit for this, but have no idea how to implement it. Here is my code which populates only Name property but not Data navigation property.

public static async Task<List<CountryDto>> ToDtosAsync(this IQueryable<Country> source, string locale)
{
    if(source == null)
    {
        return null;
    }

    var result = await source
        .Select(src => new CountryDto
        {    
           Name = src.NamesLocalized.FirstOrDefault(n => n.Locale == locale).Name
        })
        .ToListAsync();

    return result; 
}

回答1:


This answer gave me the hint for my solution. You need to use LINQKit and build Expression to convert the navigation property.

public static Expression<Func<CountryData, CountryDataDto>> ConverterExpression = cd => new CountryDataDto
        {
            Population = cd.Population
        };

public static async Task<List<CountryDto>> ToDtosAsync(this IQueryable<Country> source, string locale)
{
    if(source == null)
    {
        return null;
    }

    var result = await source
        .AsExpandable
        .Select(src => new CountryDto
        {    
           Name = src.NamesLocalized.FirstOrDefault(n => n.Locale == locale).Name
           Data = ConverterExpression.Invoke(src.Data)
        })
        .ToListAsync();

    return result; 
}


来源:https://stackoverflow.com/questions/36343067/convert-entity-with-navigation-property-to-dto-using-iqueryable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!