如何破坏包含三个列表的List元素,这样我就可以创建一个列表来添加到数据库中

时间:2018-03-05 06:46:14

标签: c# asp.net entity-framework asp.net-web-api

webAPI的内容是这个JSON字符串,并且已经变成了这个:

List<AddAssignMealView> mealtraiDeserializeObjects = JsonConvert.DeserializeObject<List<AddAssignMealView>>(mealTrainee);

mealtraiDeserializeObjects包含一周中每天的五个索引。在该特定索引内部是一个类似于以下内容的类:

public class AddAssignMealView
{
    public int TraineeID { get; set; }
    public string DayOfTheWeek { get; set; }
    public List<string> MealTypes { get; set; }
    public List<int> MealID { get; set; }
    public List<string> MealName { get; set; }
}

我要做的是,能够创建MealTrainee(实体框架)列表:

 public partial class MealTrainee
{
    public int MealTraineeID { get; set; } //Ignore this one due to it being a post
    public int MealID { get; set; }
    public int TraineeID { get; set; }
    public string DayOfTheWeek { get; set; }
    public string MealType { get; set; }
    public string MealName { get; set; }

    public virtual Meal Meal { get; set; }
}

所以我可以使用addrange并将列表添加到数据库中。我理解如何使用zip并将两个列表类型组合在一起(如果它是单个元素)。这是不同的,因为它是五个元素的列表,并且这五个元素中的每一个都包含三个列表。如果有人能指出我正确的方向,那将会有所帮助。

2 个答案:

答案 0 :(得分:3)

你可以Zip 2次将3个列表中的值组合成一系列元组。您可以使用SelectMany来平展结果。例如:

var result = mealtraiDeserializeObjects.SelectMany(c =>
    c.MealID.Zip(c.MealName, (id,name) => new {id, name})
    .Zip(c.MealTypes, (prev, type) => new {prev.id, prev.name, type})
    .Select(r => new MealTrainee
    {
        TraineeID = c.TraineeID,
        DayOfTheWeek = c.DayOfTheWeek,
        MealID = r.id,
        MealName = r.name,
        MealType = r.type,
    }));

答案 1 :(得分:0)

这是我找到的解决方案。我认为第一次迭代将是星期一,然后是用餐次数(例如:早餐用餐,午餐用餐),并将它们放入计算中,这将是每周一次的用餐时间。#perweek&#34; 。然后我每周吃一顿饭,为每个计数创造一个新的餐馆。然后,我从饭柜中取出饭后,我把它放在db.MealTrainees.AddRange中,并张贴了所有记录。

    [ResponseType(typeof(MealTrainee))]
    public IHttpActionResult PostMealTrainee([FromBody] string mealTrainee)
    {
        List<MealTrainee> meals = new List<MealTrainee>();

        using (DbContextTransaction dbContextTransaction = db.Database.BeginTransaction())
        {
            try
            {
                List<AddAssignMealView> mealtraiDeserializeObjects = JsonConvert.DeserializeObject<List<AddAssignMealView>>(mealTrainee);

                foreach (var mealtraiDeserializeObject in mealtraiDeserializeObjects)
                {
                    var mealWeek = mealtraiDeserializeObject.MealID.Select((m, i) => new
                    {
                        mealtraiDeserializeObject.TraineeID,
                        mealtraiDeserializeObject.DayOfTheWeek,
                        MealID = m,
                        MealTypes = mealtraiDeserializeObject.MealName[i],
                        MealName = mealtraiDeserializeObject.MealTypes[i]
                    }).ToList();

                 var meal = mealWeek.Select(x => new MealTrainee()
                    {
                        DayOfTheWeek = x.DayOfTheWeek,
                        MealID = x.MealID,
                        MealName = x.MealName,
                        MealType = x.MealTypes,
                        TraineeID = x.TraineeID
                    }).ToList();

                    db.MealTrainees.AddRange(meal);
                }

                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                db.SaveChanges();
                dbContextTransaction.Commit();
                return Ok(meals);
            }
            catch (Exception e)
            {
                dbContextTransaction.Rollback();
                Logger.Log(e);
                return BadRequest();
            }
        }
    }