Dynamic LINQ in the collection?

I have a project that asks me to make such a BIG search engine, but which is dynamic. I mean, I can have from 0 to 9 main “groups” that have something like the infinite possibility of “where” with “OR” or “AND”. The first thing we think is to use Dynamic Linq, which are a good alternative for building a dynamic query. All this using EF with homemade packaging.

Problem : I cannot access the "Collections". I mean, I can easily access the reference object (for example, Customer.State.StateName = "New-York" or Custoemr.State.StateName = "Quebec"), but I cannot find a way to access what something like: Customer.Orders.OrderID = 2 OR Customer.Orders.OrderID = 3 ". I can easily understand this because of my collection, but how can I do it?

Please help me!

** Sorry for my English!!


Update

I don’t know, I think I'm sorry because I'm French ...

My problem is that nothing is static. Its a candidate search engine for a recruiting company that puts candidates for the enterprise. On the page where the manager can search for a candidate, he can "understand" by: Domain (Jobs), City (cities) or many others that the user filled in when he registered. All this in the format (if it was in SQL):

[...] WHERE (domaine.domainID = 3 OR domaine.domainID = 5 OR domaine.domainID = 23) AND (cities.cityID = 4, cities.city = 32) [...]

Therefore, I cannot do this with the usual LINQ format, for example:

Candidat.Domaines.Where(domain => domain.DomainID == 3 || domain.DomainID == 5 || domain.DomainID == 23);

Even the operator in the apiary is dynamic ("AND" or "OR")! This is why we are trying to use Dynamic Linq because it is much more flexible.

Hope its easier to understand my problem ...


Update 2 Here is my method

private string BuildDomainsWhereClause() {
        StringBuilder theWhere = new StringBuilder();

        if (this.Domaines.NumberOfIDs > 0) {
            theWhere.Append("( ");

            theWhere.Append(string.Format("Domaines.Where( "));
            foreach (int i in this.Domaines.ListOfIDs) {
                if (this.Domaines.ListOfIDs.IndexOf(i) > 0) {
                    theWhere.Append(string.Format(" {0} ", this.DispoJours.AndOr == AndOrEnum.And ? "&&" : "||"));
                }
                theWhere.Append(string.Format("DomaineId == {0}", i));
            }
            theWhere.Append(" ))");
        }

        return theWhere.ToString();
    }

, "Boolean". , ? : " " Boolean "".

- : "(Domaines.Where(DomaineId == 2 && DomaineId == 3 && DomaineId == 4 && DomaineId == 5))." LINQ:

var queryWithWhere = from c in m_context.Candidats.Where(WHERE)
                                     select c;

, 7 8 "" ... ?

+5
5

, LambdaExpression ( Expression<Func<T, bool>>). . , :

ParameterExpression p = Expression.Parameter(typeof(Domaine), "domaine");
Expression<Func<Domaine, bool>> wherePredicate = 
  Expression.Lambda<Func<Domaine, bool>>(
    Expression.Or(
      Expression.Equal(
        Expression.Property(p, "DomainID"),
        Expression.Constant(10)),
      Expression.Equal(
        Expression.Property(p, "DomainID"),
        Expression.Constant(11))
      ), p);

.,

domaine.DomainID = 10 || domaine.DomainID = 11

, .

, # Visual Studio 2008 MSDN, Query. ( LinqDataSource .)

+5

, , .

private string BuildDomainsWhereClause() {
        StringBuilder theWhere = new StringBuilder();

        if (this.Domains.NumberOfIDs > 0) {
            theWhere.Append("( ");

            foreach (int i in this.Domains.ListOfIDs) {
                if (this.Domains.ListOfIDs.IndexOf(i) > 0) {
                    theWhere.Append(string.Format(" {0} ", this.Domains.AndOr == AndOrEnum.And ? "&&" : "||"));
                }
                theWhere.Append(string.Format("Domains.Any(IdDomaine== {0})", i));
            }
            theWhere.Append(" )");
        }

        return theWhere.ToString();
    }

- : "(DispoJours.Any(IdDispo == 3) && DispoJours.Any(IdDispo == 5))".

"Where builder" "& &" .

:

var queryWithWhere = from c in m_context.Candidats.Where(WHERE)
                     select c;

WHOOOHOOO!! , . ! !


, Dynamic Linq . LINQ.

+4

, Customer.Orders , .

LINQ , , OrderID ( ), :

Customer.Orders.Find(order => order.OrderID == 2);

: id 2 3 :

Customer.Orders.FindAll(order => order.OrderID == 2 || order.OrderID == 3);
+1

, , Orders , State () ?

var q = from a in Customer
    from b in a.Orders
    where b.ID == 2
              || b.ID == 3
    select b;

, .

Edit

- . , , , ,

public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values);

DynamicQueryable.

this.CountrySitesObject.Sites.AsQueryable().Where(w.WhereQuery, w.WhereParameters) 

( ).

+1

, .

Filter bug information.

Why not export data in Excel or Excel to an SQL table. It's not so much fun to build, but you do it in a couple of hours, not days or weeks. :)

0
source

All Articles