I have a method that excepts an IEnumerable<T> and a lambda expression that describes the field to be used to compare a linq-to-sql collection to an array. The method returns the matching records.
public IEnumerable<ZipCode> match<T>(IEnumerable<T> values,
Func<ZipCode, T> matchPhrase) {
return (from zipCode in _table
where values.Contains<T>(matchPhrase)
select zipCode).Distinct();
}
I’m getting the error:
Argument type
'Func<ZipCode, T>'is not assignable to parameter type'T'
The method would be called like so (where values is an IEnumerable<string> and x.zipcode is a string):
var zipCodes = _zipCodeRepository.match(values, x => x.zipcode)
UPDATE
Based on John’s suggestion of using HashSet<T> I have changed my code however I’m getting a different error now
Method ‘System.Object DynamicInvoke(System.Object[])’ has no supported translation to SQL.
I think I may not have been clear on my question and I think I’m using the wrong method signature to get my desired outcome. Let me explain with a more simple code example:
public IEnumerable<ZipCode> match(IEnumerable<string> values) {
return (from zipCode in _table
where values.Contains(zipCode.zipcode)
select zipCode).Distinct();
}
I am tyring to accomplish this but with anonymous types. I would like to pass in the field to be used in the Contains() via a lambda. So zipCode.zipcode would be passed into the method as the second argument: x => x.zipcode
I suspect you wanted to call the delegate:
Mind you, that would potentially be very expensive. You might want to create a set first:
(I’ve removed the query expression here as it was doing more harm than good in terms of readability.)