umutozel / dynamicqueryable Goto Github PK
View Code? Open in Web Editor NEWλ Construct Linq queries using strings.
License: MIT License
λ Construct Linq queries using strings.
License: MIT License
I'm trying to do something similar and it doesn't seem to work:
query.OrderBy($"{ob.Field}==null")
Any ideas?
Originally posted by @gbreen12 in #1 (comment)
2.0.25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
namespace DynamicQueryable.Nesting
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public int? Number { get; set; }
public Address Address { get; set; }
public Person()
{
Address = new Address();
}
}
public class Address
{
public string City { get; set; }
public int Zip { get; set; }
public int? Number { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Req.
// DynamicQueryable 2.0.25
// Jokenizer 1.1.1
var searchText = "1";
var data = new List<Person>()
{
new Person() { Name = "proof search 1 me" },
new Person() { Name = "This has age 1", Age = 1 },
new Person() { Name = "This has non null number 1", Number = 1 },
new Person() { Name = "This address has zip 1", Address = new Address() { Zip = 1 } },
new Person() { Name = "This address has non numm number 1", Address = new Address() { Number = 1 } }
};
var r1 = data.AsQueryable().Where($"i => i.Name != null && i.Name.Contains(\"{searchText}\")").ToList();
var r1p = data.AsQueryable().Where("i => i.Name != null && i.Name.Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
var r1Ordered = r1.AsQueryable().OrderByDescending("i=> i.Name").ToList();
// nullable vs non nullable int
var r2p = data.AsQueryable().Where("i => i.Age != null && i.Age.ToString().Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
var r2np = data.AsQueryable().Where("i => i.Number != null && i.Number.ToString().Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
// nested nullable vs non nullable int
var r3p = data.AsQueryable().Where("i => i.Address != null && i.Address.Zip != null && i.Address.Zip.ToString().Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
var r3np = data.AsQueryable().Where("i => i.Address != null && i.Address.Number != null && i.Address.Number.ToString().Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
Console.WriteLine("Hello World!");
}
}
}
Same as with standard .NET System.Dynamic.Linq.Core
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
namespace System.Linq.Dynamic
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public int? Number { get; set; }
public Address Address { get; set; }
public Person()
{
Address = new Address();
}
}
public class Address
{
public string City { get; set; }
public int Zip { get; set; }
public int? Number { get; set; }
}
class Program
{
static void Main(string[] args)
{
var searchText = "1";
var data = new List<Person>()
{
new Person() { Name = "proof search 1 me" },
new Person() { Name = "This has age 1", Age = 1 },
new Person() { Name = "This has non null number 1", Number = 1 },
new Person() { Name = "This address has zip 1", Address = new Address() { Zip = 1 } },
new Person() { Name = "This address has non numm number 1", Address = new Address() { Number = 1 } }
};
var r1 = data.AsQueryable().Where($"i => i.Name != null && i.Name.Contains(\"{searchText}\")").ToList();
var r1p = data.AsQueryable().Where("i => i.Name != null && i.Name.Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
var r1Ordered = r1.AsQueryable().OrderBy("i=> i.Name descending").ToList();
// nullable vs non nullable int
var r2p = data.AsQueryable().Where("i => i.Age != null && i.Age.ToString().Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
var r2np = data.AsQueryable().Where("i => i.Number != null && i.Number.ToString().Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
// nested nullable vs non nullable int
var r3p = data.AsQueryable().Where("i => i.Address != null && i.Address.Zip != null && i.Address.Zip.ToString().Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
var r3np = data.AsQueryable().Where("i => i.Address != null && i.Address.Number != null && i.Address.Number.ToString().Contains(searchText)", new Dictionary<string, object> { { "searchText", searchText } }).ToList();
Console.WriteLine("Hello World!");
}
}
}
System.NullReferenceException: 'Object reference not set to an instance of an object.'
this query does not work
organizationalRoles = organizationalRoles.Where("(Parent!=null ? parent.Name:Title)");
how did you handle null values in code
2.0.25
The simplest sample code to reproduce:
var sample = new[] { 1, 2, 3, 4, 5 };
var result = sample.AsQueryable().Where($"i => @0.Contains(i)", sample).ToList();
Execution and the desired items to be selected (originally you'd call the .Where on a DBSet)...
Tokenizer fails on .Contains:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Jokenizer.Net
StackTrace:
at Jokenizer.Net.ExtensionMethods.<>c__DisplayClass9_0.<GetExtensionMethod>b__0(MethodInfo m)
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at Jokenizer.Net.ExtensionMethods.GetExtensionMethod(Type forType, String name, Int32 parameterCount)
at Jokenizer.Net.TokenVisitor.GetMethod(Expression owner, String name, Int32 parameterCount)
at Jokenizer.Net.TokenVisitor.VisitCall(CallToken token, IEnumerable`1 parameters)
at Jokenizer.Net.TokenVisitor.Visit(Token token, IEnumerable`1 parameters)
at Jokenizer.Net.TokenVisitor.VisitLambda(LambdaToken token, IEnumerable`1 typeParameters, IEnumerable`1 parameters)
at Jokenizer.Net.TokenVisitor.Process(Token token, IEnumerable`1 typeParameters, IEnumerable`1 parameters)
at Jokenizer.Net.Evaluator.ToLambda(Token token, IEnumerable`1 typeParameters, IDictionary`2 variables, Object[] parameters)
at Jokenizer.Net.Evaluator.ToLambda(String token, IEnumerable`1 typeParameters, IDictionary`2 variables, Object[] parameters)
at System.Linq.Dynamic.DynamicQueryable.CreateLambda(IQueryable source, String method, String expression, Boolean generic, IDictionary`2 variables, Object[] values)
at System.Linq.Dynamic.DynamicQueryable.HandleLambda(IQueryable source, String method, String expression, Boolean generic, IDictionary`2 variables, Object[] values)
at System.Linq.Dynamic.DynamicQueryable.Where(IQueryable source, String predicate, IDictionary`2 variables, Object[] values)
at System.Linq.Dynamic.DynamicQueryable.Where[T](IQueryable`1 source, String predicate, IDictionary`2 variables, Object[] values)
at System.Linq.Dynamic.DynamicQueryable.Where[T](IQueryable`1 source, String predicate, Object[] values)
at TeamLead.Code.Services.Sync.GenericSync.Sync[TApiDto,TEntity,TId](String prefix, Int32 batchSize, Boolean shouldUpdate, Boolean shouldInsert, Boolean shouldDelete, SbApiFetchConfig`2 apiFetchConfig, DbFetchConfig`2 dbFetchConfig, Action`2 mapper) in C:\$_Work\SP.TeamLead\TeamLead\Code\Services\Sync\GenericSync.cs:line 87
2.0.25
https://gist.github.com/dcomartin/3e6fff415f6886c1b3c3b50b377fa0fc
Should behave and return the same results as the linq statement.
Throws a System.Exception: Invalid Group expression usage. This is from the Jokenizer which this package depends on.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.