将两个不同的对象列表合并为一个

时间:2019-04-17 22:27:23

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

{
    "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,但是它给出了一个无法创建类型**的常量值的例外。此内容仅支持枚举类型的原始类型。

1 个答案:

答案 0 :(得分:0)

由于类型相同,因此只需连接它们即可。请注意,在进行串联之前,您需要急切地执行查询。

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);