Linq查询无法转换为SQL:如何优化代码?

时间:2013-07-28 20:17:15

标签: asp.net mysql linq iqueryable bltoolkit

以下是我的代码的快照,工作正常:

var q1 = from msg in db.GetTable<Message>()
                .Where(msg0 => ...)
         from mt in db.GetTable<MessageTo>()
                .Where(mt0 => ...)                                       
                .DefaultIfEmpty()
         select new { msg, mt }
                    ;

AgeTypeEnum eAgeType = (AgeTypeEnum)age.Value;
switch (eAgeType)
{
case AgeTypeEnum.Invalid:
    break;

case AgeTypeEnum.LastWeek:
    q1 = q1.Where(q => q.msg.CreatedDate >= DateTime.Now.AddDays(-7));
    break;

case AgeTypeEnum.LastMonth:
    q1 = q1.Where(q => q.msg.CreatedDate >= DateTime.Now.AddMonths(-1) && q.msg.CreatedDate < DateTime.Now.AddDays(-7));
    break;

case AgeTypeEnum.CurrentSeason:
    q1 = q1.Where(q => q.msg.CreatedDate >= Season.CurrentSeason.FirstPlayedDay.RealDate && q.msg.CreatedDate < DateTime.Now.AddDays(-7));
    break;

case AgeTypeEnum.LastSeason:
    q1 = q1.Where(q => q.msg.CreatedDate >= Season.PreviousSeason.FirstPlayedDay.RealDate && q.msg.CreatedDate < Season.CurrentSeason.FirstPlayedDay.RealDate);
    break;

case AgeTypeEnum.PreviousSeasons:
    q1 = q1.Where(q => q.msg.CreatedDate < Season.PreviousSeason.FirstPlayedDay.RealDate);
    break;

default:
    throw new MyException("'{0}' age type is not supported", eAgeType);
}
return q1.Select(q => new {MessageObj = q.msg}).ToList();

这段代码工作正常,但很有点。并包含潜在的可重用逻辑。我想通过以下方式对其进行优化:

Func<Message, bool> qAgeFilter;
AgeTypeEnum eAgeType = (AgeTypeEnum)age.Value;
switch (eAgeType)
{
case AgeTypeEnum.Invalid:
    qAgeFilter = null;
    break;

case AgeTypeEnum.LastWeek:
    qAgeFilter = msg => msg.CreatedDate >= DateTime.Now.AddDays(-7);
    break;

case AgeTypeEnum.LastMonth:
    qAgeFilter = msg => msg.CreatedDate >= DateTime.Now.AddMonths(-1) && msg.CreatedDate < DateTime.Now.AddDays(-7);
    break;

case AgeTypeEnum.CurrentSeason:
    qAgeFilter = msg => msg.CreatedDate >= Season.CurrentSeason.FirstPlayedDay.RealDate && msg.CreatedDate < DateTime.Now.AddDays(-7);
    break;

case AgeTypeEnum.LastSeason:
    qAgeFilter = msg => msg.CreatedDate >= Season.PreviousSeason.FirstPlayedDay.RealDate && msg.CreatedDate < Season.CurrentSeason.FirstPlayedDay.RealDate;
    break;

case AgeTypeEnum.PreviousSeasons:
    qAgeFilter = msg => msg.CreatedDate < Season.PreviousSeason.FirstPlayedDay.RealDate;
    break;

default:
    throw new MyException("'{0}' age type is not supported", eAgeType);
}
if (qAgeFilter != null)
{
    q1 = q1.Where(q => qAgeFilter(q.msg));
}

实际上,区别在于我没有修改q1对象(查询本身),而是组合了新的委托,并在查询表达式中使用它之后。

当我尝试执行优化代码时,我收到一个异常:

  

“调用(值(vfm_elita.ServiceLayer.DataLogicLayer.Messages.MessagesExtension + LT;&GT; C_ DisplayClass21 + LT;&以及c _DisplayClass2c).qAgeFilter,   q.msg)'无法转换为SQL。

问题:

  1. 我的代码出了什么问题?

  2. 您如何建议优化源代码以提取可重复使用的逻辑以组成“过滤器”委托?

  3. 谢谢

    P.S。我使用ASP.NET 4.0,MySQL 5.0,BLToolKit作为数据库访问引擎。

1 个答案:

答案 0 :(得分:1)

对于第2点,您可以使用扩展方法

public Table<Message>      Messages             { get { return GetTable<Message>(); } }

from msg in db.Messages.ForAge(age)
                       .Where(msg0 => ...)



private static IQueryable<Message> ForAge(this IQueryable<Message> messages, AgeTypeEnum  ageType) 
{
   switch(ageType)    
   {
      case AgeTypeEnum.Invalid:   return messages;
      case AgeTypeEnum.LastWeek:  return messages.Where(q => q.msg.CreatedDate >= DateTime.Now.AddDays(-7));
      case AgeTypeEnum.LastMonth: return messages.Where(q => q.msg.CreatedDate >= DateTime.Now.AddMonths(-1) && 
                                                             q.msg.CreatedDate <  DateTime.Now.AddDays(-7));    
    } 
}

猜测你也可以在这里使用一个带有CreatedDate字段/ prop

的类的接口