LINQ: Expression.Call(typeof(Queryable), "Select"
Date : March 29 2020, 07:55 AM
should help you out I was looking more for how to do this using Expressions (correct terminology?) and I did eventually figure it out. I had to change this: IQueryable<ChildEntity> list = new List<ChildEtity>().AsQueryable();
Expression _selectExpression = Expression.Call(
typeof(Queryable),
"Select",
new Type[] { typeof(ChildEntity), typeof(ChildEntityProjection) },
Expression.Constant(list),
_nestedLambda);
MethodInfo selectMethod = null;
foreach (MethodInfo m in typeof(Enumerable).GetMethods().Where(m => m.Name == "Select"))
foreach (ParameterInfo p in m.GetParameters().Where(p => p.Name.Equals("selector")))
if (p.ParameterType.GetGenericArguments().Count() == 2)
selectMethod = (MethodInfo)p.Member;
var _selectExpression = Expression.Call(
null,
selectMethod.MakeGenericMethod(new Type[] { typeof(ChildEntity), typeof(ChildEntityProjection) }),
new Expression[] { _myPropertyExpression, _myFuncExpression });
|
Invoking HashSet.Add(item) in Linq vs foreach loop
Tag : chash , By : Thaweesak Suksuwan
Date : March 29 2020, 07:55 AM
may help you . You Select it, but don't do anything with the result. Because of LINQ's lazy evaluation (it generates the results on-demand through an IEnumerable) it just won't get executed. Use the foreach loop, it's the cleanest possible solution. (The other solution is to use a List instead and call ForEach on that... but unless you have a really good reason for wanting to use a method with a callback, there's no advantage.)customers.UnionWith(innerCustomers);
|
Dynamic LINQ, Select function, works on Enumerable, but not Queryable
Tag : chash , By : RichAA
Date : March 29 2020, 07:55 AM
hop of those help? The problem is that LineItems is an ICollection , while the first parameter to Queryable.Select requires an IQueryable. ICollection only implements IEnumerable which is why it works for Enumerable.Select. You will need to change the type of expr to be IQueryable.public static Expression<Func<decimal?>> CreateSumLineItemsExpr(Document document)
{
var docExpr = Expression.Constant(document);
var itemsExpr = Expression.Property(docExpr, "LineItems");
Expression<Func<LineItem, decimal?>> selector = i => i.Credit;
var queryableExpr = Expression.Call(typeof(Queryable), "AsQueryable", new[] { typeof(LineItem) }, itemsExpr);
var selectExpr = Expression.Call(typeof(Queryable), "Select", new[] { typeof(LineItem), typeof(decimal?) }, queryableExpr, selector);
var sumExpr = Expression.Call(typeof(Queryable), "Sum", null, selectExpr);
return Expression.Lambda<Func<decimal?>>(sumExpr);
}
|
Linq Where then Select on a HashSet
Date : March 29 2020, 07:55 AM
I wish this help you Your query returns an enumerable that corresponds to all your products, each item of that enumerable is an enumerable itself, containing zero or more categories with ID of 35. You can change your query to get only products that have category 35 in them: var products = repo
.GetAll()
.Where(p => p.CategoryProducts.Any(c => c.CategoryId == 35));
|
How can i convert linq in queryable select?
Tag : chash , By : simonth
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have this select new LINQ query : , Not sure what you exactly need. But maybe it is something like this? var listaClienti=contestoDB.Clienti.AsQueryable()
.Where(c=>c.Cognome.Contains(nominativoDaCercare) || c.Cognome == nominativoDaCercare || c.Cognome.StartsWith(nominativoDaCercare) || c.Nome.Contains(nominativoDaCercare) || c.Nome == nominativoDaCercare || c.Nome.StartsWith(nominativoDaCercare))
.Select(c=>new
{
IDCliente = c.ID,
Cliente = c.Nome + " " + c.Cognome,
Indirizzo = c.Indirizzo,
Telefono = c.Telefono,
CodiceFiscale = c.CodiceFiscale,
PartitaIva = c.PartitaIva,
NumeroVeicoli = f.CalcolaNumeroVeicoli(c.ID)
});
|