Request for an object (example)

I am looking for a tool that will dynamically generate Linq to Entity queries from a given object, Query By Entity (Example) if you want. If it belongs to the object and the context of the object, the generator returns ObectQuery or IQueryable, which can be further modified or executed. Ideally, the query builder will not directly refer to the Entity Model; rather, it will use the object context to construct the query from the model. I assume the code looks something like this:

        QueryBuilder qb = new QueryBuilder(new EntitiesContext());
        Customer c = new Customer();
        qb.Add(c);  
        c.FirstName = "Jim";
        var qry = qb.BuildQuery();
        int total = qry.Count();

The main request will look something like this:

var query = from c in ctx.Customers
            where c.FirstName == "Jim"
            select c;

Is there such a thing somewhere else? I can imagine something like that, but I would rather use what already exists.

+3
3

QueryBuilder. , , . , , , Linq .

, ? , , , ,

IQueryable<Customers> source=context.Customers;

if (...)
{
  source = from c in source
           where c.FirstName.StartsWith("Jim")
           select c;
}

if (...)
{
  source = from c in source
           where contries.Contains(c.Country)
           select c;
}

// ...
+1

Linq Linq - Linq ( ). , QueryBuilder, , .

, Linq , Linq "", ( SQL Linq-To-SQL, Linq-For-Objects). , , Linq, , , . , Linq , - .

. 65 :

Dim qry = From customer in DataContext.Customers
qry = qry.Where( Function (c as Customer) c.Name="Jim" )
qry = qry.Where( Function (c as Customer) c.Age < 65 )
qry = qry.OrderByDescending( Function (c as Customer) c.Age )
qry = qry.Take(1) 
Dim oldJim as Customer = qry.FirstOrDefault()

A FirstOrDefault method, such as using ToArray or For Each, disables the enumerator and thus processes the request.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1705275/


All Articles