If using EF Core 7, consider using json columns, the following provides a simple example, note in code OwnsOne and OwnsMany.
Getting Started: Entity Framework Core 7 JSON Support
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have data in a database column such as:
;kenworth;peterbilt;volvo
I have my LINQ statement such as:
public Task<JsonResult> GetTrucks()
{
var results = _context.Trucks
.Select(x => new
{
id = x.Id,
make = x.Makes,
model = x.Models
});
return new JsonResult(results.ToList());
}
My JSON looks like this from the above query:
is there a way to split the makes field and still have them associated with the correct ID? The Makes will be used to populate a dropdown on the UI
If using EF Core 7, consider using json columns, the following provides a simple example, note in code OwnsOne and OwnsMany.
Getting Started: Entity Framework Core 7 JSON Support
@TheCoder, thanks for the feedback, you could refer to Viorel 's advice, to use the string.split() method to get your wanted json string. Since he posted it as a comment and his code has some problems, I will give the correct code example.
var results = context.Trucks
.Select(x => new
{
id = x.Id,
make = x.Makes.Split(new string[] {";"}, StringSplitOptions.RemoveEmptyEntries)
});
var result=new JsonResult(results.ToList());
string json = new JavaScriptSerializer().Serialize(results);
Console.WriteLine(json);
Tested Result:
Hope my advice could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.