How can I set LINQ SelectMany projection via Func parameter? - c#

I have a function which returns a list of property values from a collection:
public static List<string> GetSpeakerList()
{
var Videos = QueryVideos(HttpContext.Current);
return Videos.Where(v => v.Type == "exampleType"
.SelectMany(v => v.SpeakerName)
.Distinct()
.OrderBy(s => s)
.ToList();
}
I'd like to have a generic version which will let me determine which field I'd like projected - say instead of SpeakerName I'd like to allow selecting Video.Length or Video.Type.
I understand that SelectMany takes a Func, so what's the best way to make the Func configurable to allow passing it as a parameter into this function?

Add the function as a parameter to the method.
public static List<string> GetVideosAttribute( Func<Video,string> selector )
{
var Videos = QueryVideos(HttpContext.Current);
return Videos.Where(v => v.Type == "exampleType"
.Select( selector )
.Distinct()
.OrderBy(s => s)
.ToList();
}
var speakers = GetVideosAttribute( v => v->SpeakerName );
var topics = GetVideosAttribute( v => v->Topic );

Related

NEST won't allow me to add a func parameter

Okay, I have a weird issue and I'm not sure if it's just because they're funcs and funcs are quite new to me (the whole idea of a func annoys me a bit).
I need to call the exact same query twice, because I need 2 graphs/data sets. One for each parameter. Because I don't want to duplicate my query, I've added a func parameter with my class, so I can strongly typed select the correct property on my class.
SearchDescriptor<Stats> Query(SearchDescriptor<Stats> qc, Func<Stats, double> field)
{
var query = qc
.Aggregations(aggs => aggs
.DateHistogram("histogram", d => d
.Field(f => f.Timestamp)
.FixedInterval("30m")
.MinimumDocumentCount(1)
.Aggregations(childAggs => childAggs
.Average("3", f => f
.Field(fi => field(fi))
)
)
)
)
.Query(q =>
q.Bool(b =>
b.Filter(
f => f.MatchPhrase(mp => mp
.Field(fi => fi.Name)
.Query(name)
),
f => f.DateRange(r => r
.Field(fi => fi.Timestamp)
.GreaterThanOrEquals(DateTime.UtcNow.AddDays(-1))
.LessThanOrEquals(DateTime.UtcNow)
)
)
)
);
return query;
}
var cpu = await GetByScrolling<Stats>(client, qc => Query(qc, fi => fi.CpuPercent));
var memory = await GetByScrolling<Stats>(client, qc => Query(qc, fi => fi.MemoryUsage));
This compiles and should work, but unfortunately the generated JSON turns out like this:
"aggs": {
"3": {
"avg": {
"field": "field"
}
}
}
As you can see, the value is field, which is definitely wrong. It's like the internal part of NEST does some kind of .ToString() on whatever argument is there, which is why it's returning field instead of what I put in my func.
Any idea if this is supposed to be like this? The aggs in my JSON also corresponds to the .Aggregations(aggs => aggs lambda naming.
Ah, as I said, I am new to funcs and also Expression, which turns out is what I need. It doesn't actually evaluate the func - I need to give it an expression.
SearchDescriptor<Stats> Query(SearchDescriptor<Stats> qc, Expression<Func<Stats, double>> field)
{
......
}
and then give it the expression:
.Average("3", f => f
.Field(field)
)
Then it evaluates it and works perfectly.

Build dynamic Lambda Expression for comparing undefined number of values

In short what I want do accomplish is to load Tasks from a project in SharePoint Project Server using CSOM.
var projects = ctx.LoadQuery(ctx.Projects
.Where(p => p.Id == projGuid)
.Include(
p => p.Id, p => p.Name,
p => p.Tasks
.Where(t => t.Id == taskGuid)
.Include(t => t.Name))
);
ctx.ExecuteQuery();
My Problem is with this part .Where(t => t.Id == taskGuid). It works how it should if I only want to load 1 Task but would not work if I want to load more then one. Sure I could write it like that .Where(t => t.Id == taskGuid1 || t.Id == taskGuid2 || ... )
But that wouldn't be dynamic.
What I tried was to use an array and the look if the array GuidArray.Contains(p.Id)
But I get an error if I try to use .Contains() inside the Where() expression.
ClientRequestException: The 'Contains' member cannot be used in the expression.
So I was thinking if it is possible to somehow create the lambda expression based on the number of tasks I want to load.
I regards to creation of lambda, you create the dynamic or condition you are looking for like so
public static class ExpressionExt
{
public static IQueryable<T> Where<T,TKey>(this IQueryable<T> data, string prop,params TKey[] guids)
{
var param = Expression.Parameter(typeof(T));
var exp = guids.Select(g => Expression.Equal(Expression.Property(param, prop), Expression.Constant(g))).Aggregate((a, e) => a != null ? Expression.Or(e, a) : e);
var lambda = Expression.Lambda<Func<T, bool>>(exp, param);
return data.Where(lambda);
}
}
And use it like Where(nameof(A.Id), guids) this is what I usually do when the IQueryable only supports or and not contains. There might a contains implementation there so you might want to check the documentation.

The type of the arguments cannot be inferred usage Linq GroupJoin

I'm trying to make a linq GroupJoin, and I receive the fore mentioned error. This is the code
public Dictionary<string, List<QuoteOrderline>> GetOrderlines(List<string> quoteNrs)
{
var quoteHeadersIds = portalDb.nquote_orderheaders
.Where(f => quoteNrs.Contains(f.QuoteOrderNumber))
.Select(f => f.ID).ToList();
List<nquote_orderlines> orderlines = portalDb.nquote_orderlines
.Where(f => quoteHeadersIds.Contains(f.QuoteHeaderID))
.ToList();
var toRet = quoteNrs
.GroupJoin(orderlines, q => q, o => o.QuoteHeaderID, (q => o) => new
{
quoteId = q,
orderlines = o.Select(g => new QuoteOrderline()
{
Description = g.Description,
ExtPrice = g.UnitPrice * g.Qty,
IsInOrder = g.IsInOrder,
PartNumber = g.PartNo,
Price = g.UnitPrice,
ProgramId = g.ProgramId,
Quantity = (int)g.Qty,
SKU = g.SKU
}).ToList()
});
}
I suspect this is the immediate problem:
(q => o) => new { ... }
I suspect you meant:
(q, o) => new { ... }
In other words, "here's a function taking a query and an order, and returning an anonymous type". The first syntax simply doesn't make sense - even thinking about higher ordered functions, you'd normally have q => o => ... rather than (q => o) => ....
Now that won't be enough on its own... because GroupJoin doesn't return a dictionary. (Indeed, you don't even have a return statement yet.) You'll need a ToDictionary call after that. Alternatively, it may well be more appropriate to return an ILookup<string, QuoteOrderLine> via ToLookup.

Cannot convert IQueryable<IEnumerable<string>> to return type IEnumerable<string>

In the following function I get an error when I try to return a value saying:
Cannot convert
System.Linq.IQueryable<System.Collections.Generic.IEnumerable<string>>
to return type System.Collections.Generic.IEnumerable<string>
public IEnumerable<string> GetModuleKindPropertyNames(long moduleKindId)
{
var configurationId = GetModuleKindPertypeConfigurationId(moduleKindId);
var propertyNames = _dbSis.ModuleKinds
.Where(t => t.PerTypeConfigurationId == configurationId)
.Select(x => x.PerTypeConfiguration.Properties
.Select(z => z.Name));
return propertyNames; //red line below property names
}
How can I solve this issue?
It looks like you want to select items from a collection within a collection and flatten the result into a single sequence.
Conceptually, something like:
If that's the case, you're looking for the SelectMany method:
var propertyNames = _dbSis.ModuleKinds
.Where(t => t.PerTypeConfigurationId == configurationId)
.SelectMany(x => x.PerTypeConfiguration.Properties)
.Select(z => z.Name);

Get NHibernate QueryOver .SelectList(x) from Function

Is there a way to get a list of members from a function that can be passed in to SelectList()?
So instead of doing this
var dtos = repository.QueryOver<MicrofilmExportProcessed>()
.SelectList(list => list
.Select(x => x.Member1).WithAlias(() => dto.Member1)
.Select(x => x.Member2).WithAlias(() => dto.Member2)
.Select(x => x.Member3).WithAlias(() => dto.Member3))
.List<MicrofilmExportProcessed>();
Doing something like this:
var dtos = repository.QueryOver<MicrofilmExportProcessed>()
.SelectList(getMembersFromFunc())
.List<MicrofilmExportProcessed>();
I tried creating method that returns the same type as the input parameter of the SelectList but it still tells me the input type is invalid. Not sure what I'm missing.
Something like
Func<QueryOverProjectionBuilder<InvoiceDto>, QueryOverProjectionBuilder<InvoiceDto>> GetList()
{
InvoiceDto dto = null;
return list => list.Select(w => w.Client).WithAlias(() => dto.Client);
}
and call it like
return Session.QueryOver<InvoiceDto>()
.SelectList(GetList())
.TransformUsing(Transformers.AliasToBean<InvoiceDto>())
.List<InvoiceDto>();

Categories