Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I build Entity Framework queries dynamically?

I'm quite new to Entity Framework and I have a question about filtering data.

I have two various Log entities, they are: DiskLog and NetworkLog. These entities both are derived from Log entity. Here is some code from my 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 ...
}

Of course, I already have working app and database tables created. What I want to do is to make function GetWithFilter work. I have several execution ways there:

  1. if logType == LogType.Disk && computerId <= 0 (it means there is no need to use computerId parameter in the query, select DiskLog entities only)

  2. if logType == LogType.Disk && computerId > 0 (means I have to use computerId parameter, select DiskLog entities only)

  3. if logType == LogType.NotInitialized && computerId <= 0 (no need to use computerId and logType, just select all the entities, DiskLog and NetworkLog)

  4. if logType == LogType.NotInitialized && computerId > 0 (select all types of logs for specified computer)

  5. if logType == LogType.Network && computerId <= 0 (select all NetworkLog entities)

  6. if logType == LogType.Network && computerId > 0 (select all NetworkLog entities for specified computer)

As you can see, there are plenty of available options. And I got to write 6 queries like this:

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

So the question is how can I optimize the code? Where is the way to make it better.

like image 870
Roman Pushkin Avatar asked May 04 '11 09:05

Roman Pushkin


2 Answers

You can use Func<T,bool> to optimize this

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

Then use:

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

And you can create factory for this functions

like image 53
Stecya Avatar answered Nov 01 '22 06:11

Stecya


You can easily use query compossition.

You first start with query.

IQueryable<Log> query = context.LogSet;

They you compose sub-queries.

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;

And I would recomend using nullable value types for cases, when there is no value.

like image 32
Euphoric Avatar answered Nov 01 '22 06:11

Euphoric