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);
}

「メソッド'BooleanValidEmail(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 に答える