dynamic-linq

Dynamic LINQ with direct user input, any dangers?

时光怂恿深爱的人放手 提交于 2019-12-03 17:20:42
问题 I have a table in a ASP.NET MVC application that I want to be sortable (serverside) and filterable using AJAX. I wanted it to be fairly easy to use in other places and didn't feel like hardcoding the sorting and filtering into query expressions so I looked for a way to build the expressions dynamically and the best way to do this I found was with Dynamic LINQ. User input from a URL like below is directly inserted into a dynamic Where or OrderBy. /Orders?sortby=OrderID&order=desc&CustomerName

Creating a dynamic Linq select clause from Expressions

馋奶兔 提交于 2019-12-03 16:26:51
Let's say I have defined the following variables: IQueryable<MyClass> myQueryable; Dictionary<string, Expression<Func<MyClass, bool>>> extraFields; // the dictionary is keyed by a field name Now, I want to tack on some dynamic fields to the IQueryable, so that it returns an IQueryable<ExtendedMyClass> , where ExtendedMyClass is defined as: class ExtendedMyClass { public MyClass MyObject {get; set;} public IEnumerable<StringAndBool> ExtraFieldValues {get; set;} } class StringAndBool { public string FieldName {get; set;} public bool IsTrue {get; set;} } In other words, for every value in

Dynamic Or Clause Linq

最后都变了- 提交于 2019-12-03 08:12:23
Today we currently have a statement like this: var Query = (from dp in db.Patients select dp); var UserID = User.Identity.GetUserId(); if (User.IsInRole("Administrator")) { Query = Query.Where(x => x.AdministratorID == UserID); } if (User.IsInRole("Counselor")) { Query = Query.Where(x => x.CounselorID == UserID); } if (User.IsInRole("Physician")) { Query = Query.Where(x => x.PhysicianID == UserID); } The problem is we have Users that can have multiple roles. If a User is both an Counselor and Physician we want the system to pull back all patients where CounselorID == UserID or PhysicianID ==

Dynamic where clause in LINQ - with column names available at runtime

假如想象 提交于 2019-12-03 03:02:07
Disclaimer: I've solved the problem using Expressions from System.Linq.Expressions, but I'm still looking for a better/easier way. Consider the following situation : var query = from c in db.Customers where (c.ContactFirstName.Contains("BlackListed") || c.ContactLastName.Contains("BlackListed") || c.Address.Contains("BlackListed")) select c; The columns/attributes that need to be checked against the blacklisted term are only available to me at runtime. How do I generate this dynamic where clause? An additional complication is that the Queryable collection (db.Customers above) is typed to a

How to use Dynamic LINQ (System.Linq.Dynamic) for LIKE operation?

孤人 提交于 2019-12-02 23:38:41
Can any body tell me how can I use a LIKE operator using System.Linq.Dynamic ? I need to add more than one LIKE expression in my dynamic where query /* var query = db.Customers. Where("CityName Like @0 or CityName Like @1", "London", "USA") */ var query = db.Customers. Where("CityName Like @0 or CityName Like @1%", "London", "USA") thanks heaps Try using simply "CityName.Contains(@1)" this will convert to the proper lambda since its a method invocation on an accessible type. something like: var query = db.Customers. Where("CityName.Contains(@0) or CityName.Contains(@1)", "London", "USA") Just

Invoking Regex.IsMatch() inside a dynamic linq query

限于喜欢 提交于 2019-12-02 07:45:07
I'm trying to invoke the Regex.IsMatch() and evaluate the returned result inside a dynamic linq query. This is what I tried: public static LambdaExpression Parse(SearchQuery query) { string compilableExpression = "Regex.IsMatch(Category.ToLower(), \"\\bSomeCat\\b\", RegexOptions.Compiled) == true"; ParameterExpression parameter1 = System.Linq.Expressions.Expression.Parameter(typeof(EventListItem)); ParameterExpression parameter2 = System.Linq.Expressions.Expression.Parameter(typeof(Regex)); return System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { parameter1, parameter2 }, null,

dynamically create lambdas expressions + linq + OrderByDescending

ぐ巨炮叔叔 提交于 2019-12-02 04:02:09
问题 how can I create a dynamic lambda expression to pass to use in my orderby function inside linq? I basically want transform queryResults.OrderByDescending(); in queryResults.OrderByDescending(myCustomGeneratedLambdaExp); where myCustomGeneratedLambdaExp shall be a string containning x => x.name . Thanks 回答1: I'm not sure where exactly did you need dynamic lambda expressions. Anyways, the best way to generate lambda expressions dynamically is by using expression trees. Here are two good

Dynamic Linq + Entity Framework: datetime modifications for dynamic select

让人想犯罪 __ 提交于 2019-12-02 03:06:05
问题 I am trying to find a way to move UTC time to Local before doing a sql grouping. I am using the System.Linq.Dynamic (managed here https://github.com/kahanu/System.Linq.Dynamic ). It works great for doing dynamic selects without having at compile time the required fields. In our case, we store all datetimes in UTC. In this dynamic select, its possible that someone would want to do a groupby on the Hour, year, month, etc. We have to move the data to a local time in this case, to prevent

Dynamic Linq to Xml example

倖福魔咒の 提交于 2019-12-02 02:53:57
I need a basic example on how to use System.Linq.Dynamic with Xml. Here’s a functioning statement I want to convert to dynamic Linq: XElement e = XElement.Load(new XmlNodeReader(XmlDoc)); var results = from r in e.Elements("TABLES").Descendants("AGREEMENT") where (string)r.Element("AGRMNT_TYPE_CODE") == "ISDA" select r.Element("DATE_SIGNED"); foreach (var x in results) { result = x.Value; break; } Here’s the approach I am using: string whereClause = "(\"AGRMNT_TYPE_CODE\") == \"ISDA\""; string selectClause = "(\"DATE_SIGNED\")"; var results = e.Elements("TABLES").Descendants<XElement>(

dynamically create lambdas expressions + linq + OrderByDescending

别说谁变了你拦得住时间么 提交于 2019-12-02 01:56:52
how can I create a dynamic lambda expression to pass to use in my orderby function inside linq? I basically want transform queryResults.OrderByDescending(); in queryResults.OrderByDescending(myCustomGeneratedLambdaExp); where myCustomGeneratedLambdaExp shall be a string containning x => x.name . Thanks I'm not sure where exactly did you need dynamic lambda expressions. Anyways, the best way to generate lambda expressions dynamically is by using expression trees. Here are two good tutorials on the subject: http://marlongrech.wordpress.com/2008/01/08/working-with-expression-trees-part-1/ http:/