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.
Related
I need to send a lamda in function parameter
but when i do that i've error say
Expression of type 'System.Func2[KokazGoodsTransfer.Models.Receipt,System.Decimal]' cannot be used for parameter of type 'System.Linq.Expressions.Expression1[System.Func2[KokazGoodsTransfer.Models.Receipt,System.Decimal]]' of method 'System.Decimal Sum[Receipt](System.Linq.IQueryable1[KokazGoodsTransfer.Models.Receipt], System.Linq.Expressions.Expression1[System.Func2[KokazGoodsTransfer.Models.Receipt,System.Decimal]])' (Parameter 'arg1')'
example of code
var sum = await context.Receipts.GroupBy(c => c.ClientId).Select(c => new { c.Key, Sum = c.Sum(c=>c.Amount) }).ToListAsync();
it's work fine
but when i try this i see the error
Func<Receipt, Decimal> func = c => c.Amount;
var sum = await context.Receipts.GroupBy(c => c.ClientId).Select(c => new { c.Key, Sum = c.Sum(func) }).ToListAsync();
thank you
EF usually requires an expression tree to be able to translate the code into actual SQL query.
You can try something like this (though not tested, but in some cases such tricks worked as far as I remember):
Expression<Func<Receipt, Decimal>> func = c => c.Amount;
var sum = await context.Receipts
.GroupBy(c => c.ClientId)
.Select(c => new { c.Key, Sum = c.AsQueryable().Sum(func) })
.ToListAsync();
Otherwise you maybe will need either to build select statement expression manually (which is not that easy) or look into 3rd party library like LINQKit which allows to use Func's with some magic. Something along this lines:
Expression<Func<Receipt, Decimal>> func = c => c.Amount;
var sum = await context.Receipts
.AsExpandable() // or WithExpressionExpanding on the context DI set up
.GroupBy(c => c.ClientId)
.Select(c => new { c.Key, Sum = c.Sum(func.Compile()) })
.ToListAsync();
You have to use not Func but Expression<Func<Receipt, Decimal>>. But it also will be not translatable without third-party extensions. I would suggest to use LINQKit. It needs just configuring DbContextOptions:
builder
.UseSqlServer(connectionString) // or any other provider
.WithExpressionExpanding(); // enabling LINQKit extension
Then your query will work in the following way:
Expression<Func<Receipt, Decimal>> func = c => c.Amount;
var sum = await context.Receipts.GroupBy(c => c.ClientId)
.Select(c => new { c.Key, Sum = c.Sum(x => func.Invoke(x)) })
.ToListAsync();
I'm currently working on a NEST searcher for a phones database. I've had very little luck with the dynamic version of things in terms of making it so that a user can filter certain terms to search for in the frontend.
This is because NEST doesn't like replacing a field "f.something" with a variable. Due to this I've gone to static because I believe I can do that with some object instantiation.
However, now even though I'm getting valid NEST responses back they're always empty even though there's obviously a result to be had. Such as "Name" for field and "iPhone" for query. What am I missing? Thanks in advance.
P.S. The commented-out code used to have "bool" and "should" checks in but similarly I kept getting no results.
private ISearchResponse<MasterProduct> SearchThis(ElasticClient client, string query, string field, int pageSize, int recordNumber)
{
var searchLayout = new SearchRequest<MasterProduct>
{
Size = pageSize,
From = recordNumber,
Query = new MatchQuery
{
Field = field,
Query = query,
Fuzziness = Fuzziness.Auto,
PrefixLength = 2,
Lenient = true,
FuzzyRewrite = MultiTermQueryRewrite.TopTermsBlendedFreqs(10)
}
};
var searchResponse = client.Search<MasterProduct>(searchLayout);
return searchResponse;
}
/*var searchResponse = client.Search<MasterProduct>(s => s
.From(recordNumber)
.Size(pageSize)
.Query(q => q
.Match(a => a
.Field(f => f.MasterProductName)
.Query(query)
.Fuzziness(Fuzziness.Auto)
.PrefixLength(2)
.Fuzziness(Fuzziness.Auto)
.Lenient()
.FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10))
)
.Match(b => b
.Field(f => f.ManufacturerName)
.Query(query)
.Fuzziness(Fuzziness.Auto)
.PrefixLength(2)
.Fuzziness(Fuzziness.Auto)
.Lenient()
.FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10))
)
.Match(c => c
.Field(f => f.MasterAttributes)
.Query(query)
.Fuzziness(Fuzziness.Auto)
.PrefixLength(2)
.Fuzziness(Fuzziness.Auto)
.Lenient()
.FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10))
)
)
);
Console.WriteLine(searchResponse.Hits.Count());
foreach (var hit in searchResponse.Documents)
{
Console.WriteLine(hit.MasterProductId);
}*/
}
#RussCam answered the question in the comments above.
To reiterate, pass only strings in as parameters to fields, not other datatypes.
Camelcase is also necessary for this to work.
I have a string like string strn = "abcdefghjiklmnopqrstuvwxyz" and want a dictionary like:
Dictionary<char,int>(){
{'a',0},
{'b',1},
{'c',2},
...
}
I've been trying things like
strn.ToDictionary((x,i) => x,(x,i)=>i);
...but I've been getting all sorts of errors about the delegate not taking two arguments, and unspecified arguments, and the like.
What am I doing wrong?
I would prefer hints over the answer so I have a mental trace of what I need to do for next time, but as per the nature of Stackoverflow, an answer is fine as well.
Use the .Select operator first:
strn
.Select((x, i) => new { Item = x, Index = i })
.ToDictionary(x => x.Item, x => x.Index);
What am I doing wrong?
You're assuming there is such an overload. Look at Enumerable.ToDictionary - there's no overload which provides the index. You can fake it though via a call to Select:
var dictionary = text.Select((value, index) => new { value, index })
.ToDictionary(pair => pair.value,
pair => pair.index);
You could try something like this:
string strn = "abcdefghjiklmnopqrstuvwxyz";
Dictionary<char,int> lookup = strn.ToCharArray()
.Select( ( c, i ) => new KeyValuePair<char,int>( c, i ) )
.ToDictionary( e => e.Key, e => e.Value );
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 );
Why does this yield an empty set?
Object[] types = {23, 234, "hello", "test", true, 23};
var newTypes = types.Select(x => x.GetType().Name)
.Where(x => x.GetType().Name.Equals("Int32"))
.OrderBy(x => x);
newTypes.Dump();
When you do your select you're getting an IEnumerable<String>. Then you're taking the types of each string in the list (which is all "String") and filtering them out where they aren't equal to "Int32" (which is the entire list). Ergo...the list is empty.
Equals works just fine, it's your query that isn't correct. If you want to select the integers in the list use:
var newTypes = types.Where( x => x.GetType().Name.Equals("Int32") )
.OrderBy( x => x );
Reverse the order of the operations:
var newTypes = types.Where(x => x is int)
.OrderBy(x => x)
.Select(x => x.GetType().Name);
(Notice this also uses a direct type check instead of the rather peculiar .GetType().Name.Equals(…)).
The thing with LINQ is you've got to stop thinking in SQL terms. In SQL we think like this:-
SELECT Stuff
FROM StufF
WHERE Stuff
ORDER BY Stuff
That is what your code looks like. However in LINQ we need to think like this :-
FROM Stuff
WHERE Stuff
SELECT Stuff
ORDER BY Stuff
var newTypes = types.Select(x => x.GetType().Name)
.Where(x => x.Equals("Int32"))
.OrderBy(x => x);
This doesn't work because the Select statement will convert every value in the collection to the name of the underlying type of that value. The resulting collection will contain only string values and hence they won't ever have the name Int32.