0

我有以下方法:

public List<Alert> GetMonthlyAlertsByAccountID(Int32 AccountID, params int[] alertTypes)
        {
            List<Alert> result = new List<Alert>();

            using (NeuroLabLinqDataContext dc = conn.GetContext())
            {
                IEnumerable<Alert> alerts = (from a in dc.Alerts
                                             where a.AccountID == AccountID &&
                                             a.AlertTypeID.In(alertTypes)
                                             orderby ((DateTime)a.CreateDate).Month ascending
                                             select a).ToList();
            }

            return result;
        }

使用扩展方法:

public static bool In<T>(this T t, params T[] values)
        {
            foreach (T value in values)
            {
                if (t.Equals(value))
                {
                    return true;
                }
            }
            return false;
        }

其目的是仅返回具有某些 AlertTypeID 的项目。

结果是:

方法 'Boolean In[Int32](Int32, Int32[])' 不支持对 SQL 的转换。

我确信不必使用扩展方法就可以解决问题。有人可以指出我正确的方向吗?

谢谢。

4

1 回答 1

4

采用:

alertTypes.Contains(a.AlertTypeID)

反而。

IEnumerable<Alert> alerts = (from a in dc.Alerts
     where a.AccountID == AccountID &&
     alertTypes.Contains(a.AlertTypeID)
     orderby ((DateTime)a.CreateDate).Month ascending
     select a).ToList();
于 2012-05-22T22:54:43.150 回答