如何动态构建实体框架查询?

时间:2011-05-04 09:10:53

标签: c# linq entity-framework filter filtering

我对Entity Framework很陌生,我对过滤数据有疑问。

我有两个不同的Log实体,它们是:DiskLogNetworkLog。这些实体都来自Log实体。以下是我的C#app中的一些代码:

public class Log { ... }
public class DiskLog : Log { ... }
public class NetworkLog : Log { ... }

public enum LogType
{
    NotInitialized = 0,
    Disk,
    Network
}

public List<Log> GetWithFilter(
    Guid userKey, 
    int nSkip, 
    int nTake, 
    DateTime dateFrom = DateTime.MinValue, 
    DateTime dateTo = DateTime.MaxValue, 
    LogType logType = LogType.NotInitialized, 
    int computerId = 0)
{
    // need to know how to optimize ...

    return ...
}

当然,我已经创建了工作应用和数据库表。我想要做的是使函数GetWithFilter工作。我有几种执行方式:

  1. if logType == LogType.Disk && computerId <= 0(表示查询中不需要使用computerId参数,只选择DiskLog实体)

  2. if logType == LogType.Disk && computerId > 0(表示我必须使用computerId参数,仅选择DiskLog实体)

  3. if logType == LogType.NotInitialized && computerId <= 0(无需使用computerId和logType,只需选择所有实体,DiskLog和NetworkLog)

  4. if logType == LogType.NotInitialized && computerId > 0(为指定的计算机选择所有类型的日志)

  5. if logType == LogType.Network && computerId <= 0(选择所有NetworkLog实体)

  6. if logType == LogType.Network && computerId > 0(选择指定计算机的所有NetworkLog实体)

  7. 如您所见,有很多可用选项。我要编写6个这样的查询:

    1

    context.LogSet
        .OfType<DiskLog>
        .Where(x => x.Computer.User.UserKey == userKey)
        .Where(x => x.DateStamp >= dateFrom && x.DateStamp < dateTo)
        .OrderByDescending(x => x.Id)
        .Skip(nSkip)
        .Take(nTake)
        .ToList();
    

    2

    context.LogSet
        .OfType<DiskLog>
        .Where(x => x.Computer.User.UserKey == userKey)
        .Where(x => x.DateStamp >= dateFrom && x.DateStamp < dateTo)
        .Where(x => x.Computer.Id == computerId)
        .OrderByDescending(x => x.Id)
        .Skip(nSkip)
        .Take(nTake)
        .ToList();
    

    3

    context.LogSet
        .Where(x => x.Computer.User.UserKey == userKey)
        .Where(x => x.DateStamp >= dateFrom && x.DateStamp < dateTo)
        .OrderByDescending(x => x.Id)
        .Skip(nSkip)
        .Take(nTake)
        .ToList(); // simplest one!
    

    4

    context.LogSet
        .Where(x => x.Computer.User.UserKey == userKey)
        .Where(x => x.DateStamp >= dateFrom && x.DateStamp < dateTo)
        .Where( x => x.Computer.Id == computerId)
        .OrderByDescending(x => x.Id)
        .Skip(nSkip)
        .Take(nTake)
        .ToList();
    

    5

    context.LogSet
        .OfType<NetworkLog>
        .Where(x => x.Computer.User.UserKey == userKey)
        .Where(x => x.DateStamp >= dateFrom && x.DateStamp < dateTo)
        .OrderByDescending(x => x.Id)
        .Skip(nSkip)
        .Take(nTake)
        .ToList();
    

    6

    context.LogSet
        .OfType<NetworkLog>
        .Where(x => x.Computer.User.UserKey == userKey)
        .Where(x => x.DateStamp >= dateFrom && x.DateStamp < dateTo)
        .Where( x => x.Computer.Id == computerId)
        .OrderByDescending(x => x.Id)
        .Skip(nSkip)
        .Take(nTake)
        .ToList();
    

    所以问题是如何优化代码?如何使其变得更好。

2 个答案:

答案 0 :(得分:13)

您可以轻松使用查询组合。

首先从查询开始。

IQueryable<Log> query = context.LogSet;

他们组成子查询。

if (logType == LogType.Disk)
{
    query = query.OfType<DiskLog>(); // not sure if you need conversion here
} 
else if (logType == LogType.Network)
{
    query = query.OfType<NetworkLog>(); // not sure if you need conversion here
}

query = query.Where(x => x.Computer.User.UserKey == userKey);

if (computerId != 0)
   query = query.Where( x => x.Computer.Id == computerId);

// .. and so on

query = query.OrderByDescending(x => x.Id).Skip(nSkip).Take(nTake);

return query.ToList(); // do database call, materialize the data and return;

当没有值时,我会建议使用可空值的类型。

答案 1 :(得分:7)

您可以使用Func<T,bool>优化此

IEnumerable<T> Select<T>(IEnumerable<T> source, Func<T, bool> userKeyFunc, Func<T, bool> dateFunc, int skip, int take)
{
    return source.OfType<T>().Where(userKeyFunc).Where(dateFunc).Skip(skip).Take(take);
}

然后使用:

var result = Select<NetworkLog>(context.LogSet,x => x.Computer.User.UserKey == userKey,
                                x => x.DateStamp >= dateFrom && x.DateStamp < dateTo, nSkip,nTake)

您可以为此功能创建工厂