0

我想从我的联系人表中返回联系人记录,但仅限于联系人电子邮件地址有效的地方。所以类似于以下内容:

var contacts = (from cont in db.Contacts
                        where cont.Accounts_CustomerID == accountId
                        && ValidEmail(cont.EmailAddress)
                        select new ContactLight
                        {
                            AccountId = cont.Accounts_CustomerID,
                            FirstName = cont.Firstname,
                            LastName = cont.Lastname,
                            EmailAddress = cont.EmailAddress
                        });

private static bool ValidEmail(string email)
{
    if(email == "")
        return false;
    else
        return new System.Text.RegularExpressions.Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,6}$").IsMatch(email);
}

我收到“方法 'Boolean ValidEmail(System.String)' 不支持对 SQL 的转换。” 所以我假设无法翻译带有正则表达式的方法。解决这个问题的最佳方法是什么?

4

1 回答 1

3

将您的电子邮件验证移至客户端(Linq-to-Objects)。

var contacts = db.Contacts
    .Where(cont.Accounts_CustomerID == accountId)
    .Select(cont => new ContactLight
                    {
                        AccountId = cont.Accounts_CustomerID,
                        FirstName = cont.Firstname,
                        LastName = cont.Lastname,
                        EmailAddress = cont.EmailAddress
                    })
    .AsEnumerable() //this forces request to client side
    .Where(e => ValidEmail(e.EmailAddress));
于 2012-08-03T10:50:16.737 回答