ASP.NET check if LinqToEntities returned something or not
How to make this method return boolean value, depending on query return. False - nothing, True - data exists. Before i just returned int from uniqueQuote.Count() but does not look like great method. Thank you!
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 == cu开发者_开发技巧rrentUserId
select quot;
bool exist = uniqueQuote;
return exist;
Use the Enumerable.Any method:
return uniqueQuote.Any();
Or pass the predicate to it directly:
return ctx3.Enquiries.Include("aspnet_Users")
.Any(quot => quot.EnquiryId == selectedEnquiryId
&& quot.aspnet_Users.UserId == currentUserId);
I'm more used to this format, but you can translate to use .Any
return ctx3.Enquiries.Include("aspnet_Users")
.Any(x=> x.EnquiryId == selectedEnquiryId &&
x.aspnet_Users.UserId == currentUserId);
Try something like:
return uniqueQuote.Count() > 0;
精彩评论