Asked by:
Check if any data equals string (true/false) problem.

Question
-
User1643232994 posted
bool exceptionExist = _context.OrderArticle.Any(x => x.State == "Exception");
if (!exceptionExist)
{
//Do something
}
Not working :'(
Wednesday, December 13, 2017 3:21 PM
All replies
-
User-1838255255 posted
Hi thorran,
According to your description and code, the query sentence is right, you say not working, could you tell us where not working or if occur some error message? please share us more useful information.
Best Regards,
Eric Du
Thursday, December 14, 2017 8:03 AM -
User1643232994 posted
One example:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SetHandled(int? id, string orderNo)
{
if (id == null || orderNo == null)
{
return NotFound();
}
var orderArticle = _context.OrderArticle.Where(s => s.Id == id).SingleOrDefault();
orderArticle.State = "OK";
_context.OrderArticle.Update(orderArticle);
_context.SaveChanges();
bool exceptionExist = _context.OrderArticle.Any(x => x.State == "Exception"); //THIS ALWAYS RETURN FALSE
if (!exceptionExist)
{
var order = _context.Order.Where(s => s.OrderNo == orderNo).SingleOrDefault();
order.State = "OK";
order.HandledDate = Convert.ToDateTime(DateTime.Now);
_context.Order.Update(order);
_context.SaveChanges();
}
_context.SaveChanges();
return RedirectToAction("Index");
}
I need to check the datatable 'order', attribute 'state', if there's any value with 'Exception', if there's not and all of them have value 'ok', then I want to put the state in another datatable 'ok'. But this is never happen because "bool exceptionExist = _context.OrderArticle.Any(x => x.State == "Exception");" always return false.
Thursday, December 14, 2017 3:42 PM -
User1643232994 posted
With other words, the query string is wrong/not working.
I have also try this:
bool exceptionExist = _context.OrderArticle.Any(x => x.State.Equals("Exception"));and
bool exceptionExist = _context.OrderArticle.FirstOrDefault(x => x.State == "Exception") != null;
Does anyone know how to write the query correctly?
Thursday, December 14, 2017 4:07 PM -
User2103319870 posted
bool exceptionExist = _context.OrderArticle.Any(x => x.State.Equals("Exception"));Consider doing a case insenstive search like below
bool exceptionExist = _context.OrderArticle.Any(x => x.State.ToString().ToLower().Equals("exception"));
Thursday, December 14, 2017 4:37 PM -
User1643232994 posted
Thanks! But not working :(
Thursday, December 14, 2017 5:01 PM -
User-1838255255 posted
Hi thorran,
The query sentence is right, i have tested it in my side, it returns true. You say, it doesn't work, if your meaning is that your table exist the value equal to "Exception" in State column? If my understanding is right, please try to check if exist value(Exception) in State column!
Best Regards,
Eric Du
Tuesday, December 19, 2017 7:37 AM -
User753101303 posted
Hi,
For now it seems you really don't have matching data in your db. Have you tried to give a closer look at your data, for example maybe extra trailing whitespaces ?
You are 100% sure it is not entered or could it be also that "// Do something" doesn't work as you expect making you to think it doesn't happen at all???
Tuesday, December 19, 2017 7:59 AM