【问题标题】:Merge two different Object list into one将两个不同的对象列表合并为一个
【发布时间】:2019-09-08 05:53:01
【问题描述】:
{
    "food": [
        {
            "ID": 65,
            "Name": "Grilled chicken",
            "Price": "580",
            "CatID": 75,
            "UID": 101,
            "Date_Time": "2019-04-01T00:00:00",
            "FoodDescription": "Chicken with some oregeno",
            "CookingTime": "25 min"
},
        {
            "ID": 67,
            "Name": "Chicken manchurian",
            "Price": "480",
            "CatID": 77,
            "UID": 101,
            "Date_Time": "2019-04-01T00:00:00",
            "FoodDescription": "fried chicken balls in manchurian sauce",
            "CookingTime": "1 hour 20 min"
}
    ],
    "rate": [
        {
            "ID": 34,
            "Name": "Seekh Kabab",
            "Price": "700",
            "CatID": 47,
            "UID": 102,
            "Date_Time": "2019-01-08T00:00:00",
            "FoodDescription": "Seekh kabab with pakistani Masala and Garlic Sauce.",
            "CookingTime": "3 Hours"}
    ]
}

我不希望我的 json 像这样返回。 我想实现这个

 {
            "ID": 65,
            "Name": "Grilled chicken",
            "Price": "580",
            "CatID": 75,
            "UID": 101,
            "Date_Time": "2019-04-01T00:00:00",
            "FoodDescription": "Chicken with some oregeno",
            "CookingTime": "25 min"
},
        {
            "ID": 67,
            "Name": "Chicken manchurian",
            "Price": "480",
            "CatID": 77,
            "UID": 101,
            "Date_Time": "2019-04-01T00:00:00",
            "FoodDescription": "fried chicken balls in manchurian sauce",
            "CookingTime": "1 hour 20 min"
},

        {
            "ID": 34,
            "Name": "Seekh Kabab",
            "Price": "700",
            "CatID": 47,
            "UID": 102,
            "Date_Time": "2019-01-08T00:00:00",
            "FoodDescription": "Seekh kabab with pakistani Masala and Garlic Sauce.",
            "CookingTime": "3 Hours"}
    ]
}

通过这个 C# 代码,我得到了这些数据,请帮助我,我是编码新手。提前致谢。

public HttpResponseMessage recommendfood(int id)
        {

            List<int> order_track = db.Order_Trackings.Where(e => e.UID == id).Select(q => q.ID).ToList();
            List<Food> rates= db.Foods.OrderByDescending(f => f.Ratings.Max(r => r.Rate)).ToList();


            if (order_track.Count==0)
            {
                var rate = rates.Take(4).Distinct();
                return Request.CreateResponse(HttpStatusCode.OK, rate);
            }
          List<int> fidList = db.OrderFoods.Where(q => order_track.Contains(q.OID)).Select(q => q.FID).ToList();
            var qs = (from x in fidList
                      group x by x into g
                      let count = g.Count()
                      orderby count descending
                      select new { KEY = g.Key });


            if (order_track.Count == 1)
            {
                var one = qs.Take(1);

                List<int> idList = new List<int>();
                foreach (var val in one)
                {
                    idList.Add(val.KEY);
                }
                var foodQuery = db.Foods.Where(row => idList.Contains(row.ID));
                var rateQuery = rates.Where(row => !foodQuery.Any(food => food.ID == row.ID)).Take(3);
                var result = new
                {
                    food = foodQuery,
                    rate = rateQuery
                };

                return Request.CreateResponse(HttpStatusCode.OK, result);
            }
            if (order_track.Count == 2)
            {
                var two = qs.Take(2);
                List<int> idList = new List<int>();
                foreach (var val in two)
                {
                    idList.Add(val.KEY);
                }
                var foodQuery = db.Foods.Where(row => idList.Contains(row.ID));
                var rateQuery = rates.Where(row => !foodQuery.Any(food => food.ID == row.ID)).Take(2);
                var result = new
                {
                    food = foodQuery,
                    rate = rateQuery
                };
                return Request.CreateResponse(HttpStatusCode.OK, result);
            }
            if (order_track.Count >= 3)
            {
                var three = qs.Take(3);
                List<int> idList = new List<int>();
                foreach (var val in three)
                {
                    idList.Add(val.KEY);
                }
                var foodQuery = db.Foods.Where(row => idList.Contains(row.ID));
                var rateQuery = rates.Where(row => !foodQuery.Any(food => food.ID == row.ID)).Take(1);
                var result = new
                {
                    food = foodQuery,
                    rate = rateQuery
                };

                return Request.CreateResponse(HttpStatusCode.OK,result);
            }

            return Request.CreateResponse(HttpStatusCode.InternalServerError,"Sorry Something Bad Happend");
        }
    }

我尝试使用 concat,但它给出了无法创建类型**的常量值的异常。此内容仅支持枚举类型的原始类型。

【问题讨论】:

  • 由于这两个对象的类型相同,创建一个具有所需属性的类,实例化该类的列表并将 foodQuery 和 rateQuery 中的项目添加到列表中,然后返回结果。

标签: c# asp.net json linq web-services


【解决方案1】:

由于类型相同,您可以将它们连接起来。请注意,您需要在进行连接之前急切地执行查询。

var foods = db.Foods.Where(row => idList.Contains(row.ID)).ToArray();
var rates = rates.Where(row => !foodQuery.Any(food => food.ID == row.ID)).Take(2).ToArray();

var result = new List<Food>(foods.Concat(rates));
return Request.CreateResponse(HttpStatusCode.OK, result);

【讨论】:

  • 非常感谢这是正确的答案,但请您详细说明为什么使用 .ToArray()
  • @HanzalaIqbal 因为您无法连接两个实体框架查询,所以您需要先将结果存储到内存中,并且由于您要创建一个列表,使用ToList 将分配您赢得的两个列表'不使用
猜你喜欢
  • 1970-01-01
  • 2018-04-21
  • 2021-10-29
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多