两个查询之间的区别

时间:2019-04-14 09:58:53

标签: c# entity-framework linq

我想在食物和费率之间使用区别。

我希望该费率表中的食品ID以FID的形式存在,所以请跳过该ID,希望您理解我的问题。

 var result = new
                {
                    food = db.Foods.Where(q => idList.Contains(q.ID)),
                    rate = rates.Take(1).Distinct()
                };

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

我要得到3个食物对象和一个评分对象,我想跳过食品对象中已经存在FID的那个评分对象,对此我无法进一步详细说明。

"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",
            "Image": ,
            "Uploadedby": "Hanzala Iqbal",
            "Carts": [],
            "Category": null,
            "User": null,
            "FoodRecommendations": [],
            "OrderFoods": [],
            "Ratings": []
        },
        {
            "ID": 69,
            "Name": "Lahori chargha",
            "Price": "1000",
            "CatID": 79,
            "UID": 101,
            "Date_Time": "2019-04-01T00:00:00",
            "FoodDescription": "Garnish with some tomato sauce ",
            "CookingTime": "2 hours",
            "Image": ",
            "Uploadedby": "Hanzala Iqbal",
            "Carts": [],
            "Category": null,
            "User": null,
            "FoodRecommendations": [],
            "OrderFoods": [],
            "Ratings": []
        },
        {
            "ID": 70,
            "Name": "Moroccon chicken",
            "Price": "900",
            "CatID": 80,
            "UID": 101,
            "Date_Time": "2019-04-01T00:00:00",
            "FoodDescription": "chicken with green olives and lemon",
            "CookingTime": "2.5 hour",
            "Image": "",
            "Uploadedby": "Hanzala Iqbal",
            "Carts": [],
            "Category": null,
            "User": null,
            "FoodRecommendations": [],
            "OrderFoods": [],
            "Ratings": []
        }
    ],
    "rate": [
        {
            "ID": 15,
            "Rate": 5,
            "FID": 65,
            "UID": 102,
            "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",
                "Image": "",
                "Uploadedby": "Hanzala Iqbal",
                "Carts": [],
                "Category": null,
                "User": null,
                "FoodRecommendations": [],
                "OrderFoods": [],
                "Ratings": []
            },
            "User": null
        }
    ]

2 个答案:

答案 0 :(得分:0)

您也可以使用以下一种:

var result = db.Foods.Where(q => q.Rates.Any(x => x.FID != q.Id));

答案 1 :(得分:0)

var foodQuery = db.Foods.Where(row => idList.Contains(row.ID));
var rateQuery = db.Rates.Where(row => !foodQuery.Any(food => food.ID == row.FID)).Take(1);

var result = new
{
    food = foodQuery,
    rate = rateQuery
};

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