2

Probably something simple, but as I'm new to lambda expressions, the problem evades me:

m => m.contactID == contactID && m.primaryAddress == true && (m.addressTypeID == 2 || m.addressTypeID == 3)

I tried to use that lambda expression but I receive an invalid operator. Is there a way to simplify this so that it would work?

Edit:

The equivolent sql query would be:

SELECT *
FROM Contact
WHERE contactID = 3
AND primaryAddress = 1
AND (addressTypeID = 2 OR addressTypeID = 3)

I have a repository function defined like so:

public E Single(Expression<Func<E, bool>> where)
{
 return objectSet.Single<E>(where);
}

I'm passing the lambda expression above into this function:

myRepository.Single(m => m.contactID == contactID && m.primaryAddress == true && (m.addressTypeID == 2 || m.addressTypeID == 3));
asked Jun 15, 2010 at 20:38
6
  • 2
    What are you trying to do? Can you post some more information/code? Commented Jun 15, 2010 at 20:40
  • 1
    Also, please post the full text of the error you're receiving. Commented Jun 15, 2010 at 20:40
  • is m.primaryAddress != null (or !string.IsNullOrEmpty(m.primaryAddress)) what you meant? EDIT: if primaryAddress is integer, you cannot compare it to true/false. Commented Jun 15, 2010 at 20:43
  • 2
    Is this a compiler error, or a runtime error? If runtime, it's most likely that there is more than a single element... Commented Jun 15, 2010 at 20:51
  • Runtime - I'll double check but I'm pretty darn positive its returning only 1 item. Commented Jun 15, 2010 at 20:55

4 Answers 4

3

If you are receiving an InvalidOperationException, the most likely cause is that there is more than one record that matches your criteria.

Queryable.Single will raise InvalidOperationException if there is more than a single correct value. In this case, try using .First(m => ..) instead:

myRepository.First(m => 
 m.contactID == contactID && 
 m.primaryAddress == true && 
 (m.addressTypeID == 2 || m.addressTypeID == 3)
 );

This will return the first matching result, if there are more than one. If you need to handle no matches, look into FirstOrDefault (which will return null if there are no matches).

answered Jun 15, 2010 at 20:49
Sign up to request clarification or add additional context in comments.

Comments

1

m.primaryAddress == true looks suspicious. is m.primaryAddress really a bool property?

answered Jun 15, 2010 at 20:41

3 Comments

its a bit operator in the database
It could be, perhaps would be better named IsPrimaryAddress. But really, You should just write "&& m.primaryAddress &&" instead of "&& m.primaryAddress == true &&", since it is a bool.
a bit operator? is it nullable?
0

m => (m.contactID == contactID && m.primaryAddress == true && (m.addressTypeID == 2 || m.addressTypeID == 3)) is merely a boolean expression.

You need to do something like list.Remove(m => (m.contactID == contactID && m.primaryAddress == true && (m.addressTypeID == 2 || m.addressTypeID == 3))) etc

This says take each item in my list as m if this returns true remove m

Edit OP reposted while I was writing that answer

I would write this like this as to your syntax.

Func Filter<var, bool> = m => (m.contactID == contactID && m.primaryAddress == true && (m.addressTypeID == 2 || m.addressTypeID == 3))

then pass Filter into your myrepository.Single(Filter)

answered Jun 15, 2010 at 20:47

Comments

0

From your SQL query, should it be:

m => m.contactID == contactID && m.primaryAddress == 1 && (m.addressTypeID == 2 || m.addressTypeID == 3)
answered Jun 15, 2010 at 20:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.