两个查询之间的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了两个查询之间的区别相关的知识,希望对你有一定的参考价值。

我想在食物和速度之间使用不同的。

我希望食物ID如果在费率表中作为FID存在,那么跳过那个我希望你理解我的问题。

 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
        }
    ]
答案
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);
另一答案

你也可以用这个:

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

以上是关于两个查询之间的区别的主要内容,如果未能解决你的问题,请参考以下文章

这两个代码片段有啥区别?

找不到使用'[bracket]'和'查询查询属性之间的区别。点'符号

这两个mysql查询之间的区别?

两个Sql查询之间的区别[重复]

活动和片段之间有啥区别?

使用“in”和两个索引之间的数组进行查询时有啥区别?