A
A
Anton2017-04-16 21:34:25
C++ / C#
Anton, 2017-04-16 21:34:25

How to assemble a request from parts?

Good!
Is it possible to assemble a query to the database from parts that will be formed based on the checked checkboxes?
For example, if the "date" checkbox is checked, then we add a condition for the date.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
d-stream, 2017-04-16
@d-stream

It is possible, but it is a difficult and winding path with a bunch of scattered rakes.
Much more direct - collect in the form or bunch (flag not cocked or condition by date)

S
Stanislav Kobelev, 2017-04-18
@StrataRozumu

You create your own class, which generates a request by analogy with the Builder pattern.
And at each stage, you pass the previous generated IQueriable if necessary.

class QueryBuilder
    {
        private IQueryable m_IQueryable;

        //Создаем запрос в конструкторе
        public QueryBuilder()
        {
            m_IQueryable = (from i in table select i);
        }
        
        //Добавляем условия
        public void AddDate(DateTime timestamp)
        {
            m_IQueryable = m_IQueryable.Where(x => x.Timestamp == timestamp);
        }

        //Возвращает сформированый запрос
        public IQueryable GetQuery()
        {
            return m_IQueryable;
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question