Git Product home page Git Product logo

Comments (11)

sdanyliv avatar sdanyliv commented on August 19, 2024

Use Sql.CharIndex() function.

from linq2db.entityframeworkcore.

quan01994 avatar quan01994 commented on August 19, 2024

List is externally passed in, the internal does not know how much length, how to modify the middle ‘or’

from linq2db.entityframeworkcore.

quan01994 avatar quan01994 commented on August 19, 2024

ok, i know how to write ,Preset a few values

from linq2db.entityframeworkcore.

sdanyliv avatar sdanyliv commented on August 19, 2024

I'll post solution in 5 minutes

from linq2db.entityframeworkcore.

quan01994 avatar quan01994 commented on August 19, 2024

:) ,ok

from linq2db.entityframeworkcore.

sdanyliv avatar sdanyliv commented on August 19, 2024

There is:

public static class DynamicExtensions
{
	private static IQueryable<T> WhereByItems<T, TV>(IQueryable<T> query, IEnumerable<TV> items, 
		Expression<Func<T, TV, bool>> predicate, Func<Expression, Expression, Expression> binaryFunc)
	{
		if (items == null) throw new ArgumentNullException(nameof(items));
		if (predicate == null) throw new ArgumentNullException(nameof(predicate));
		if (binaryFunc == null) throw new ArgumentNullException(nameof(binaryFunc));

		Expression body = null;
		foreach (var item in items)
		{
			var constant = Expression.Constant(item);
			var current =
				predicate.Body.Transform(e => e == predicate.Parameters[1] ? constant : e);
			body = body == null ? current : binaryFunc(body, current);
		}

		return body == null ? query : query.Where(Expression.Lambda<Func<T, bool>>(body, predicate.Parameters[0]));
	}

	public static IQueryable<T> WhereOr<T, TV>(this IQueryable<T> query, IEnumerable<TV> items, Expression<Func<T, TV, bool>> predicate)
	{
		return WhereByItems(query, items, predicate, Expression.OrElse);
	}

	public static IQueryable<T> WhereAnd<T, TV>(this IQueryable<T> query, IEnumerable<TV> items, Expression<Func<T, TV, bool>> predicate)
	{
		return WhereByItems(query, items, predicate, Expression.AndAlso);
	}
}

And usage:

using (var db = new HYWCRMContext(connectionString))
{
    //this list  is param,
    var list = new string[] { "0-6", "7-9" };
    // MemberResTagNames nvarchar is '0-6,10-12'
    var temp = db.MemberInfo
        .WhereOr(list, (m, i) => m.MemberResTagNames.IndexOf(i)>0)
        .ToList();
}

from linq2db.entityframeworkcore.

sdanyliv avatar sdanyliv commented on August 19, 2024

Solution is based on our function Transform. If you need pure mplementation, you have to use visitors.

from linq2db.entityframeworkcore.

quan01994 avatar quan01994 commented on August 19, 2024

add it to next version?

from linq2db.entityframeworkcore.

sdanyliv avatar sdanyliv commented on August 19, 2024

Nope, it is not suitable for this project. Almost every experinced programmer has these extensions.
We are thinking about additional library which may contain such helper functions.

from linq2db.entityframeworkcore.

quan01994 avatar quan01994 commented on August 19, 2024

ok, my programming skills are weak.

from linq2db.entityframeworkcore.

sdanyliv avatar sdanyliv commented on August 19, 2024

So just copy and enjoy ;)

from linq2db.entityframeworkcore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.