0

I need to write a linq query to check an ID is exist in sql table and If the ID is exist need to check one field is null or not in the same table.

I am new in entity framework ,please help.

Currently doing by like this:-

Here i am selecting by two methods,if the first method returns true i am checking the second method. Based on this value i need to enable/disable a tab.

public bool GetCompanyInfoById(int customerId)
        {
            using (var context = new eSmoEntities())
            {
                bool companyExist = context.tm_cmd_company_details.Any(x=>x.com_id.Equals(customerId));
                return companyExist;
            }
        }

        public bool GetGroupForCompany(int customerId)
        {
        using (var context = new eSmoEntities())
            {
                var customer = context.tm_cmd_company_details.Where(x => x.com_id == customerId).Select(x=>x.cmd_group_id).FirstOrDefault();
                return customer != null ? true : false;
            }
        }
1
  • 1
    It would help if you show your entity definition and/or a sample query (working or not). Usually such a condition can be done with a join clause similar to where new {x.A, x.B} equals new {y.A, y.B} Commented May 26, 2016 at 12:36

2 Answers 2

2

This is a pretty trivial question, you just need two boolean conditions for your linq query:

Context.MyItems.Any(i => i.ID == suppliedID && i.OtherProperty != null)

Obviously, this will be easier to answer specifically if you supply your EntityFramework model.

This is only half the problem though; what do you want to do after? Select an item that meets your specifications? Do something if these conditions are met? It appears you need to think through your problem on your own before coming to Stack Overflow and asking for a complete solution for you to copy-paste.

Sign up to request clarification or add additional context in comments.

Comments

0

You have first to find the entity with the given Id, for example =1.

var entity = dbContext.AnyTable.FirstOrDefault(t => where t.Id =1);
if (entity != null)
{

  if (entity.AnyField != null)
  {

   } 
}

In this apporach you must use eager loading. If you are using Lazy loading then the entity will be load at runtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.