Изменить, как показано ниже:
public async Task<ActionResult<Form>> GetFormByName(string name, bool includeSettings = false, bool includeFields = false)
{
var varForm = _context.Forms
.Where(current => current.Name.Contains(name))
.AsQueryable();
if (includeSettings == true)
{
varForm = varForm
.Include(current => current.FormUiSetting)
.Include(current => current.FormResultSetting);
}
if (includeFields == true)
{
varForm = varForm
.Include(current => current.FieldItems)
.ThenInclude(field => field.Validations)
.Include(current => current.FieldItems)
.ThenInclude(field => field.Options);
varForm.FirstOrDefaultAsync().Result.FieldItems.Options = varForm.FirstOrDefaultAsync()
.Result.FieldItems.Options
.OrderBy(o => o.Index).ToList();
}
return await varForm.SingleOrDefaultAsync();
}
Моя модель:
public class Form
{
public int Id { get; set; }
public string Name { get; set; }
public FormUiSetting FormUiSetting { get; set; }
public FormResultSetting FormResultSetting { get; set; }
public FieldItem FieldItems { get; set; }
}
public class FormUiSetting
{
public int Id { get; set; }
public string FormUiName { get; set; }
}
public class FormResultSetting
{
public int Id { get; set; }
public string FormResultName { get; set; }
}
public class FieldItem
{
public int Id { get; set; }
public string FieldItemName { get; set; }
public Validation Validations { get; set; }
public List<Options> Options { get; set; }
}
public class Validation
{
public int Id { get; set; }
public string ValidateName { get; set; }
}
public class Options
{
public int Id { get; set; }
public string Index { get; set; }
}
ОБНОВЛЕНИЕ:
Для ваших требований, кажется, нет необходимости чтобы использовать FieldItem
, просто используйте OptionableFieldItem
.
Модель:
public class Form
{
public int Id { get; set; }
public string Name { get; set; }
public List<OptionableFieldItem> OptionableFieldItems { get; set; }
}
public class FieldItem
{
public int Id { get; set; }
public string FieldItemName { get; set; }
public List<Validation> Validations { get; set; }
}
public class OptionableFieldItem : FieldItem
{
public List<Options> Options { get; set; }
}
public class Validation
{
public int Id { get; set; }
public string ValidateName { get; set; }
}
public class Options
{
public int Id { get; set; }
public string Index { get; set; }
}
Действия:
if (includeFields == true)
{
varForm = varForm.Include(current => current.OptionableFieldItems)
.ThenInclude(field => field.Validations)
.Include(current => current.OptionableFieldItems)
.ThenInclude(field => field.Options);
var data = varForm.FirstOrDefaultAsync().Result.OptionableFieldItems;
foreach (var item in data)
{
item.Options = item.Options.OrderBy(o => o.Index).ToList();
}
}