linq-expressions

Generic DbDataReader to List<T> mapping

女生的网名这么多〃 提交于 2019-12-17 07:41:33
问题 I am having a slight issue (more like an annoyance) with my property binding data access classes. The problem is that the mapping fails when there exists no column in the reader for corresponding property in class. Code Here is the mapper class: // Map our datareader object to a strongly typed list private static IList<T> Map<T>(DbDataReader dr) where T : new() { try { // initialize our returnable list List<T> list = new List<T>(); // fire up the lamda mapping var converter = new Converter<T>

Generic DbDataReader to List<T> mapping

不羁岁月 提交于 2019-12-17 07:41:15
问题 I am having a slight issue (more like an annoyance) with my property binding data access classes. The problem is that the mapping fails when there exists no column in the reader for corresponding property in class. Code Here is the mapper class: // Map our datareader object to a strongly typed list private static IList<T> Map<T>(DbDataReader dr) where T : new() { try { // initialize our returnable list List<T> list = new List<T>(); // fire up the lamda mapping var converter = new Converter<T>

Build a simple Expression Tree in .NET

喜欢而已 提交于 2019-12-13 21:49:17
问题 I have a interface in that user indicates some elements and operators between them and I should display the result. User can build then a filter like p1 OP v1 OR p2 OP v2 where p1 and p2 are Person properties, like Age, Name, Location etc, v1 and v2 are comparative values(10, 'Maria', 'L.A.'), OP are comparison operators (=, <, >) and OR is a logical operator(can be also AND). Eg: Age > 18 AND Location = 'Paris' , or another one like Name Contains 'andro' AND Sex = 'm' Having myPeople

Combining AndAlso The parameter 'foo' was not bound in the specified LINQ to Entities query expression

拈花ヽ惹草 提交于 2019-12-13 21:08:47
问题 I have an entity. public class Foo { public int Id { get; set; } public string Name { get; set; } public string Code { get; set; } } I want to create my own expression predicate. For that I have created a method that accepts property name and the value. private static Expression<Func<Foo, bool>> Condition(string pName, object value) { var pe = Expression.Parameter(typeof(Foo), "foo"); var left = Expression.Property(pe, pName); var right = Expression.Constant(value); var equal = Expression

How to join multiple conditions for EF Expressions

こ雲淡風輕ζ 提交于 2019-12-13 19:54:06
问题 Expression<Func<Dealer, bool>> GetFilter() { Expression<Func<Dealer, bool>> f = (d) => 1 == 1; var s = ""; if (QS["Province"] != null) { s = QS["Province"]; f = d => d.Address.Province.Equals(s); } if (QS["City"] != null) { s = QS["City"]; f = d => d.Address.City.Equals(s); } if (QS["NameStartWith"] != null) { s = QS["NameStartWith"]; f = d => d.DealerName.Substring(0, 1).ToUpper().Equals(s); } return f; } Depending on querystrings I want to generate Expression for getting all the dealers

How to parse Conditional Ternary syntax (a > b ? a : b) using Sprache

倖福魔咒の 提交于 2019-12-13 16:29:02
问题 I keep thinking I understand Sprache parsing the more I use it, but then I come across a new syntax that baffles me, and there are not a lot of examples online. At this time, I'm trying to allow my string to have conditional syntax (like C#) such as: (A > B ? A : B) A+2 > 7 ? (A + 2) : -1 (A*B) < 12 ? "Real" : "Fake" I have the regular operators working fine (>,<,!,=,etc.) using the following parse: private Parser<ExpressionType> Operator(string op, ExpressionType opType) => Parse.String(op)

Using brackets in dynamic .NET expressions

一个人想着一个人 提交于 2019-12-12 17:52:06
问题 I have a grid in witch a user can fill-in the "filter" on a collection. The user has to fill-in some columns: AndOr Property Comparator Value say, for a Cities collection it could filter cities that - Name StartsWith 'a' AND Population > 10000 OR Population < 1000 I used the dynamic PredicateBuilder, that worked very well, until the "brackets" requirement appeared. As you can see from the "query" above, in the resulting collection we will have cities which (Name.StartsWith'a' AND Population >

A List of Field Names as a String Array - LINQ Expressions

不打扰是莪最后的温柔 提交于 2019-12-11 18:08:07
问题 Hello MVC and LINQ Experts, I have a Model that looks like this: public class SomeClass : IValidatableObject { public string SomeString { get; set; } public string SomeString2 { get; set; } public int SomeInteger { get; set; } public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { //... IF there is some error...THEN yield return new ValidationResult("Some Error Message.", GetFieldNames(() => new []{ this.SomeString })); } } As you can see, I am calling

Can a LINQ Expression defined as a lambda expression include other LINQ Expressions?

流过昼夜 提交于 2019-12-11 08:54:42
问题 When using LINQ Expressions, the C# compiler will conveniently translate C# lambdas into Expression objects: //using System; //using System.Linq.Expressions; Expression<Func<int, bool>> lambda_expression = (int x) => x == 3; This is convenient, and can save a lot of typing versus explicitly constructing the expression: Expression<Func<int, bool>> explicit_expression_object; { var x = Expression.Parameter(typeof(int), "x"); explicit_expression = Expression.Lambda<Func<int, bool>>(Expression

Generic method to get property values with Linq Expression and reflection

陌路散爱 提交于 2019-12-11 05:48:44
问题 Dear Gods of Reflection I would like to have a generic GetValue<TEntity, T> method that can return the following property values given the following User class: public class User { public int Id { get; set; } public int ClientId { get; set; } public string UserName { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string MobileNumber { get; set; } public bool IsActive { get; set; } public Client Client { get; set; } public List<Package> Packages {