ASP.NET检查LinqToEntities是否返回了一些内容

时间:2010-04-08 20:00:27

标签: asp.net entity-framework

如何使此方法返回布尔值,具体取决于查询返回。假 - 没有,真实 - 数据存在。在我从uniqueQuote.Count()返回int之前,但看起来不是很好的方法。谢谢!

private bool CheckEnquiryUser(int enquiryId, Guid userId)
            {
                int selectedEnquiryId = enquiryId;
                Guid currentUserId = userId;
                Entities ctx3 = new Entities();
                var uniqueQuote = from quot in ctx3.Enquiries.Include("aspnet_Users")
                                  where quot.EnquiryId == selectedEnquiryId &&
                                  quot.aspnet_Users.UserId == currentUserId

                              select quot;

            bool exist = uniqueQuote;
            return exist;

3 个答案:

答案 0 :(得分:1)

使用Enumerable.Any method

return uniqueQuote.Any();

或直接将谓词传递给它:

return ctx3.Enquiries.Include("aspnet_Users")
           .Any(quot => quot.EnquiryId == selectedEnquiryId
                       && quot.aspnet_Users.UserId == currentUserId);

答案 1 :(得分:1)

我更习惯这种格式,但您可以翻译为使用.Any

return ctx3.Enquiries.Include("aspnet_Users")
     .Any(x=> x.EnquiryId == selectedEnquiryId &&
           x.aspnet_Users.UserId == currentUserId);

答案 2 :(得分:0)

尝试类似:

return uniqueQuote.Count() > 0;