C
C#2y ago
Hercules

❔ Can't get around on how to instantiate this.

I can't get around on how to instantiate this in my service.cs so i can call it properly in my controller. Relevant code below. https://paste.mod.gg/kocfivzcagoc/0
BlazeBin - kocfivzcagoc
A tool for sharing your source code with the world!
28 Replies
phaseshift
phaseshift2y ago
Word frequency result is not word frequency
Hercules
Hercules2y ago
Yes i know they are two different types but I'd like to be able to generate Word frequency result https://paste.mod.gg/kocfivzcagoc/0
BlazeBin - kocfivzcagoc
A tool for sharing your source code with the world!
phaseshift
phaseshift2y ago
New one up
Hercules
Hercules2y ago
My Challenge is making below to return - Word frequency result @phaseshift I've tried instantiate Lists
var frequencies =
dict.OrderByDescending(x => x.Value)
.Select(x => new WordFrequency(x.Key, x.Value))
.Take(n)
.ToList();
var frequencies =
dict.OrderByDescending(x => x.Value)
.Select(x => new WordFrequency(x.Key, x.Value))
.Take(n)
.ToList();
phaseshift
phaseshift2y ago
You don't even have new wordfreqresult
Hercules
Hercules2y ago
Shouldn't something like this work?
var frequencies =
dict.OrderByDescending(x => x.Value)
.Select(x => new WordFrequencyResult(new List<List<WordFrequency>>()
{
new WordFrequency(x.Key, x.Value)
}))
.Take(n)
.ToList();
var frequencies =
dict.OrderByDescending(x => x.Value)
.Select(x => new WordFrequencyResult(new List<List<WordFrequency>>()
{
new WordFrequency(x.Key, x.Value)
}))
.Take(n)
.ToList();
phaseshift
phaseshift2y ago
Doubt it. Isn't 'result' meant to contain the lists?
Hercules
Hercules2y ago
I do have a
public record WordFrequencyResult(List<List<WordFrequency>>? ListOfWordFrequency);
public record WordFrequencyResult(List<List<WordFrequency>>? ListOfWordFrequency);
Result is meant to return the values i wish to give as output in my endpoint through Response.cs
phaseshift
phaseshift2y ago
Yeah, so that should be the last thing you new. Putting it inside linq select is going to make a collection of 'result' var lists = linq stuff Var result = new Result(lists)
Hercules
Hercules2y ago
Here is a another example but if works fine difference are the type in Result.cs https://paste.mod.gg/tpnvxlicymuu/0
BlazeBin - tpnvxlicymuu
A tool for sharing your source code with the world!
Hercules
Hercules2y ago
Exactly i need to return a List<WordFrequency> with linq. But when i do .Select(x => new (starting my instansiations) ) // i get wrong.
phaseshift
phaseshift2y ago
What type does your linq statement return?
Hercules
Hercules2y ago
List<WordFrequency> right now.
foreach (var dict in stringResult)
{
var frequencies =
dict.OrderByDescending(x => x.Value)
.Select(x => new WordFrequency(x.Key, x.Value))
.Take(n)
.ToList();
}
foreach (var dict in stringResult)
{
var frequencies =
dict.OrderByDescending(x => x.Value)
.Select(x => new WordFrequency(x.Key, x.Value))
.Take(n)
.ToList();
}
phaseshift
phaseshift2y ago
Right. And what does result take? Not that. So you need to figure out how to nest another list
Hercules
Hercules2y ago
If we break down the question. It would be, how do would you instantiate
List<List<WordFrequencyResult>>
List<List<WordFrequencyResult>>
WordFrequencyResult takes
List<List<WordFrequency>>? ListOfWordFrequency
List<List<WordFrequency>>? ListOfWordFrequency
And WordFrequency is
public record WordFrequency(string Word, long FrequencyCount);
public record WordFrequency(string Word, long FrequencyCount);
phaseshift
phaseshift2y ago
What. You want list of list of result? That's four nested lists.
Hercules
Hercules2y ago
Yes otherwise i'll break architectual pattern that i've done. like the Authentication link i sent before
phaseshift
phaseshift2y ago
Well it's dumb. But it's not complicated Frequencies is only one list So you need another list before you make a result
Hercules
Hercules2y ago
Yes. Im having incompetent problems on getting that list.
phaseshift
phaseshift2y ago
You make the list before the loop where you make frequencies Then inside that loop, you added frequencies to it Your problem is literally the same as how to add numbers to a list Just replace'numbers' with 'some other list'
Hercules
Hercules2y ago
var test = new List<List<WordFrequencyResult>>();
{
new List<WordFrequencyResult>()
{
new WordFrequencyResult(
new List<List<WordFrequency>>()
{
new List<WordFrequency>()
{
new WordFrequency("s", 2)
}
}
)
};
};
var test = new List<List<WordFrequencyResult>>();
{
new List<WordFrequencyResult>()
{
new WordFrequencyResult(
new List<List<WordFrequency>>()
{
new List<WordFrequency>()
{
new WordFrequency("s", 2)
}
}
)
};
};
4 lists yes you're right.
phaseshift
phaseshift2y ago
So problem is solved? You know how to do it
Hercules
Hercules2y ago
Nope, This gave me List<List<List<WordFrequencyResult>>>
phaseshift
phaseshift2y ago
'this'?
Hercules
Hercules2y ago
The code above xD
phaseshift
phaseshift2y ago
How You just need to pay more attention
Hercules
Hercules2y ago
Yes, I managed to solve it now, all thanks to you - it's about thinking and working on single things and not over complicate it. And you actually helped me realize that my achitecture is dumb. but i think if my application was much much bigger it wouldn't be so dumb
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.