skip to Main Content

I want to select the multiple column based on if condition, While adding "Select" method as nested it is not working, Getting null for each properties in the class. So how to fix this?

public async Task<Form?> GetFormByIdAsync(long formId, string organisationId,
    bool includeBuilderSchema = false,bool includeViewerSchema = false)
{
    var query = _dbContext.Forms.AsQueryable()
        .Where(f => f.Id.Equals(formId) && f.OrganizationId.Equals(organisationId)).Select(f =>
            new Form()
            {
                Id = f.Id,
                Name = f.Name,
                Description = f.Description,
            });

    if (includeBuilderSchema)
    {
        query = query.Select(f => new Form()
        {
            FormBuilderSchema = f.FormBuilderSchema,
        });
    }

    if (includeViewerSchema)
    {
        query = query.Select(f => new Form()
        {
            FormViewerSchema = f.FormViewerSchema,
        });
    }

    var result = await query
        .FirstOrDefaultAsync();

    return result;
}

2

Answers


  1. Chosen as BEST ANSWER

    I added condition using ternary operator while assigning fields inside Select function.

    public async Task<Form?> GetFormByIdAsync(long formId, string organisationId,
        bool includeBuilderSchema = false,
        bool includeViewerSchema = false
        )
    {
        var query = _dbContext.Forms
            .Where(f => f.Id.Equals(formId) && f.OrganizationId.Equals(organisationId)).Select(f =>
                new Form
                {
                    Id = f.Id,
                    Name = f.Name,
                    FormBuilderSchema = includeBuilderSchema ? f.FormBuilderSchema : default,
                    FormViewerSchema = includeViewerSchema ? f.FormViewerSchema : default,
                });
    
        var generatedQuery = query.ToQueryString();
    
        var result = await query
            .FirstOrDefaultAsync();
    
    
        return result;
    }
    

    When includeBuilderSchema is true, the generatedQuery is changed correctly.

    But not sure is it good coding practice.


  2. The second SELECT is overriding the first, only the FormBuilderSchema and OrganizationId props are initialized. Try:

    public async Task<Form?> GetFormByIdAsync(long formId, string organisationId,
        bool includeBuilderSchema = false)
    {
        var query = _dbContext.Forms.AsQueryable()
            .Where(f => f.Id.Equals(formId) && f.OrganizationId.Equals(organisationId));
    
        if (includeBuilderSchema)
        {
            query = query.Select(f => new Form()
            {
                Id = f.Id,
                Name = f.Name,
                Description = f.Description,
                FormBuilderSchema = f.FormBuilderSchema,
                OrganizationId = f.OrganizationId,
            });
        }
        else 
        {
            query = query.Select(f => new Form()
            {
                Id = f.Id,
                Name = f.Name,
                Description = f.Description,
            });
        }
    
        var result = await query.FirstOrDefaultAsync();
    
        return result;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search