Вот мои классы базы данных
Код: Выделить всё
public class Sign
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public int? SignGroupId { get; set; }
public SignGroup SignGroup { get; set; } = null!;
public int LocationId { get; set; }
public Location Location { get; set; } = null!;
public static Expression MapToDto()
{
return s => new()
{
Id = s.Id,
Name = s.Name ?? "",
//this call works as I would expect by translating into SQL and executing it at the
//database level.
Signs = s.SignGroup.Signs.Where(ss => ss.Id != s.Id)
.AsQueryable().Select(MapToGroupSignDto()).ToList(),
//This call will end up bringing the whole Location object into memory and then
//perform the mapping from memory.
LocationInfo = Location.MapToSignLocationDto()
.Compile().Invoke(s.Location),
};
}
public static Expression MapToGroupSignDto()
{
return gs => new()
{
Id = gs.Id,
Name = gs.Name ?? "",
};
}
}
public class SignGroup
{
public int Id { get; set; }
public List Signs { get; set; } = null!;
}
public class Location
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public string? Latitude { get; set; }
public string? Longitude { get; set; }
public string? State { get; set; }
public string? City { get; set; }
public string? Zip { get; set; }
public string? StreetAddress { get; set; }
public static Expression MapToSignLocationDto()
{
return l => new()
{
Id = l.Id,
Name = l.Name,
Latitude = l.Latitude ?? "",
Longitude = l.Longitude ?? "",
};
}
}
Код: Выделить всё
public record SignDTO
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public int SignGroupId { get; set; }
[Comment("Signs At Same Location")]
public List AdditionalSings { get; set; } = null!;
public SignLocationDTO LocationInfo { get; set; } = null!;
}
public record GroupSignDTO
{
public int Id { get; set; }
public string Name { get; set; } = null!;
}
public record LocationDTO
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public string Latitude { get; set; } = null!;
public string Longitude { get; set; } = null!;
}
Код: Выделить всё
public async Task GetListDataAsync()
{
IQueryable signIQ = _context.Signs;
Queryable dtoIQ = signIQ.Select(Sign.MapToDto());
return await dtoIQ.ToListAsync();
}
public static Expression MapToDto()
{
return s => new()
{
Id = s.Id,
Name = s.Name ?? "",
//this call works as I would expect by translating into SQL and executing it at the
//database level.
Signs = s.SignGroup.Signs.Where(ss => ss.Id != s.Id)
.AsQueryable().Select(MapToGroupSignDto()).ToList(),
//This call will end up bringing the whole Location object into memory and then
//perform the mapping from memory.
LocationInfo = Location.MapToSignLocationDto()
.Compile().Invoke(s.Location),
};
}
public static Expression MapToGroupSignDto()
{
return gs => new()
{
Id = gs.Id,
Name = gs.Name ?? "",
};
}
public static Expression MapToSignLocationDto()
{
return l => new()
{
Id = l.Id,
Name = l.Name,
Latitude = l.Latitude ?? "",
Longitude = l.Longitude ?? "",
};
}
Я пробовал разные разные способов сделать это, но единственный способ добиться того, что я хотел, — это поместить весь код в одну функцию-выражение, например:
Код: Выделить всё
public static Expression MapToDto()
{
return s => new()
{
Id = s.Id,
Name = s.Name ?? "",
Signs = s.SignGroup.Signs.Where(ss => ss.Id != s.Id)
.Select(gs => new()
{
Id = gs.Id,
Name = gs.Name ?? "",
).ToList(),
LocationInfo = new()
{
Id = s.Location.Id,
Name = s.Location.Name,
Latitude = s.Location.Latitude ?? "",
Longitude = s.Location.Longitude ?? "",
},
};
}
Будем очень признательны за любую помощь и идеи в этой области.
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/784 ... ore-to-sql
Мобильная версия