Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic metods for get data #148

Open
alex23215 opened this issue Oct 2, 2024 · 0 comments
Open

Generic metods for get data #148

alex23215 opened this issue Oct 2, 2024 · 0 comments

Comments

@alex23215
Copy link

alex23215 commented Oct 2, 2024

Hello, when generating the code for getting a collection of objects from the database. Radzen Studio creates a similar method for each model(table)

public async Task<IQueryable<Item>> GetItems(Query query = null)
{
    var items = Context.Items.AsQueryable();
    if (query != null)
    {
        if (!string.IsNullOrEmpty(query.Expand))
        {
            var propertiesToExpand = query.Expand.Split(',');
            foreach (var p in propertiesToExpand)
            {
                items = items.Include(p.Trim());
            }
        }
        if (!string.IsNullOrEmpty(query.Filter))
        {
            if (query.FilterParameters != null)
            {
                items = items.Where(query.Filter, query.FilterParameters);
            }
            else
            {
                items = items.Where(query.Filter);
            }
        }
        if (!string.IsNullOrEmpty(query.OrderBy))
        {
            items = items.OrderBy(query.OrderBy);
        }
        if (query.Skip.HasValue)
        {
            items = items.Skip(query.Skip.Value);
        }
        if (query.Top.HasValue)
        {
            items = items.Take(query.Top.Value);
        }
    }
    return await Task.FromResult(items);
}

Why don't you use something like this generics-based method to avoid code duplication

public IQueryable<TEntity> GetItems<TEntity>(Func<TEntity,bool>? expForFilter = null, Expression<Func<TEntity, dynamic>>[]? collectionIncludes = null) where TEntity : class
{
    var items = Context.Set<TEntity>().AsQueryable();

    if (collectionIncludes != null)
    {
        
        items = collectionIncludes.Where(x=>x.Body.Type != typeof(string)).Aggregate(items, (current, include) => current.Include(include));
        var stringIncludes = collectionIncludes.Where(x=>x.Body.Type == typeof(string));
        if (stringIncludes is not null && stringIncludes.Any())
        {
            foreach (var includ in stringIncludes.Select(x=>x.Body))
            {
                items = items.Include(includ.ToString());
            }
        }
    }
    
    if (expForFilter != null)
    {
        items = items.AsEnumerable().Where(expForFilter).AsQueryable();
    }
    return items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant