Linq 2 sql, enum, no supported translation to SQL

D

deerchao

Hi, I'm using linq 2 sql, and I specified a custom enum type
(CompanyType) to one of the Company table field.
When I'm querying like this:

IEnumerable<CompanyType> validTypes= ...;
var v = from c in db.Companies
where validTypes.Contains(c.CompanyType)
select c;

I got an exception says:

Method 'Boolean Contains(MyNamespace.CompanyType)' has no
supported translation to SQL.

If I use int instead of "MyNamespace.CompanyType" as the type of
property "CompanyType" (and the IEnumerable<int> validTypes),
everything works. So what should I do to make my custom enum types
work?

Thanks!
 
D

deerchao

Hi, I'm using linq 2 sql, and I specified a custom enum type
(CompanyType) to one of the Company table field.
When I'm querying like this:

IEnumerable<CompanyType> validTypes= ...;
var v = from c in db.Companies
    where validTypes.Contains(c.CompanyType)
    select c;

I got an exception says:

    Method 'Boolean Contains(MyNamespace.CompanyType)' has no
supported translation to SQL.

If I use int instead of "MyNamespace.CompanyType" as the type of
property "CompanyType" (and the IEnumerable<int> validTypes),
everything works. So what should I do to make my custom enum types
work?

Thanks!

Sorry, it's my fault.
I checked again, the validTypes is not of Type
IEnumerable<CompanyType> but HashSet<CompanyType>.
Everything works after I change the declared type of validTypes into
IEnumerable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top