tags:

views:

31

answers:

1

Hi, I am getting the following error:

Error Message:The member 'Company.ProductCore.Core.Domain.Account.Email' has no supported translation to SQL.

My method looks like this:

public Account GetAccountByEmail(string email)
        {
            Account account;

            using (WorkbookDataContext dc = _conn.GetContext())
            {
                account = ( from a in dc.Accounts
                            join em in dc.Emails on a.AccountId equals em.AccountId
                            where a.Email.EmailAddress == email
                            select a).FirstOrDefault();
            }

            return account;

        }

My Account class has a getter / setter that exposes Email:

 public Email Email
        {
            get { return _email; }
            set { _email = value; }
        }

And my Email is a LINQ object.

I have a feeling that the problem is that I am using a LINQ object for me Email property? I am new to LINQ and am not really sure why this is happening.

Help appreciated, thanks...

A: 

I think this is what you're after (you need to reference the email address directly, through a known mapping), but I can't be 100% sure without knowing your structure:

            account = (from a in dc.Accounts
                       join em in dc.Emails on a.AccountId equals em.AccountId
                       where em.EmailAddress == email
                       select a).FirstOrDefault();

The why? part:
In short, LINQ has no mapping knowledge of what that property is...and can't make a SQL query because it doesn't know, so you'll get a runtime error when it tries to generate a query from the expression tree.

You can't use a property in a query unless LINQ knows what table/column/function it maps to, without this...it simply can't convert it to or from SQL.

Nick Craver
Thanks Nick - that was it.
Code Sherpa