0

I've got the two following entities ...

class Citation { public int CitationId { get; set; } public string Identifier { get; set; } } class CitationIdentifier { public int CitationIdentifierId { get; set; } public string Identifier { get; set; } } 

I'm trying to query for all Citation records where the Identifier property does not match any of the CitationIdentifier.Identifier property. So, if I have a Citation with an Identifier property containing "foo", but there are no CitationIdentifier records with an Identifier property containing "foo", then I'd like to retrieve that Citation.

I'm working with an IDbSet<Citation>.

Any ideas? Thanks.

1
  • 2
    What queries have you tried so far, and in what way have they failed to solve your problem? Commented Jun 10, 2014 at 15:13

2 Answers 2

2

Maybe an except clause? Select all of them except the ones that share an identifier:

(from citation in Citation select citation) .Except( from c in Citation join ci in CitationIdentifier on c.Identifier equals ci.Identifier select c); 
Sign up to request clarification or add additional context in comments.

Comments

1

You could try this one:

var result = citations.Where(x=>!citationIdentifiers.Any(y=>y.Identifier==x.Identifier)); 

where I suppose that citations and citationIdentifiers are collections that contain all the citations and citation identifiers respectively you have.

Using the Any extension method, you are trying to check if there is any citation identifier with the value of identifier equal to the identifier value of the current citation record. If so, then this citation will not be inluded int the result, since we take the negation, of

citationIdentifiers.Any(y=>y.Identifier==x.Identifier) 

inside in our Where extension method.

1 Comment

At first I was chagrinned at being beaten to the answer, but today I learned to use !collection.Any(predicate) instead of collection.Where(predicate).Count == 0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.