|
It looks like you are only looking for the highest single item, not getting a complete sorted list, which is what your question seems to imply that you are looking for. But on the assumption that the .FirstOrDefault does give you what you want ...
I am not sure whether any efficiency savings would be sufficiently significant, but I would consider combining the priorities into a single value using const / enums rather than using dictionary lookups and only sorting the list once against a single key. e.g. (my C# is a little rusty, so forgive syntax errors)
[Flags]
enum YourRevisedPriority
{
SuperUser = &x0000,
NormalUser = &x0100,
Beginner = &x0200,
Internal = &x0000,
External = &x0010,
Valid = &x0000,
ValidInFuture = &x0001,
Expire = &x0002
}
TransformedProfile = profileList.Select(x => new TransformedProfile
{
ProfileType = GetProfileType(x.PropertyA),
ProfileValidity = GetValidity(x.StartDate, x.ExpiryDate),
NewPriority =
(ProfileType == SourceEnum.SuperUser ? YourRevisedPriority.SuperUser : (ProfileType == SourceEnum.NormalUser ? YourRevisedPriority.NormalUser : YourRevisedPriority.Beginner)) +
(GetProfileSubType(x.PropertyB) == SourceEnum.Internal ? YourRevisedPriority.Internal : YourRevisedPriority.External) +
(ProfileValidity == SourceEnum.Valid ? YourRevisedPriority.Valid : (ProfileValidity == SourceEnum.ValidInFuture ? YourRevisedPriority.ValidInFuture : YourRevisedPriority.Expire))
})
.OrderBy(y => y.NewPriority)
.FirstOrDefault();
|
|
|
|
|
Thanks for the suggestions! I ended up following @OriginalGriff suggestion and implemented IComparer for the TransformedProfile and IComparer for a custom sort-list, to allow for some short-circuit comparison (and also allowed me to put not-found values last in the sort).
Thanks!
|
|
|
|
|
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello all,
So basically, in my programming the user can press the default key that i have set, which is F1, to enable a feature. However, i want the user to be able to change this key. so for example, a dialog pops up and asks them to enter the key they want to set for that feature then it changes the F1 (default key) to the new key that the user chose. How do i do this?
|
|
|
|
|
Firstly, don't use F1 for that: the Windows standard use for F1 is "Help", and it's a bad idea to "play" with standard keys - the user can if he wishes, but you shouldn't as it gets confusing.
The simplest way is to have a Dictionary of keys, and Func values:
private Dictionary<Keys, Func<int>> keyFunctions = new Dictionary<Keys, Func<int>>();
private int A()
{
Console.WriteLine("A");
return 1;
}
private int B()
{
Console.WriteLine("B");
return 2;
}
private int C()
{
Console.WriteLine("C");
return 3;
}
...
keyFunctions.Add(Keys.A, A);
keyFunctions.Add(Keys.B, B);
keyFunctions.Add(Keys.C, C);
...
You can then call the method by accessing the Dictionary with the Key value:
Console.WriteLine(keyFunctions[Keys.C]());
Console.WriteLine(keyFunctions[Keys.B]());
Console.WriteLine(keyFunctions[Keys.A]());
You can use Action instead of Func if your methods do not return a value, but that makes the Add operation more complex:
keyFunctions.Add(Keys.A, () => A());
keyFunctions.Add(Keys.B, () => B());
keyFunctions.Add(Keys.C, () => C());
You can then change the Keys value as you need, and process the Dictionary in your Forms ProcessCmdKey override provided you have set the Form.KeyPreview to true .
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have a solution as graphically. I need programmatic code for enable and disable scrollbars on datagridview using C# windows application...
tnq in advanced,
Rajasekaran Bose
|
|
|
|
|
|
I am working to build up a GPIB : VISA driver for the IVI and NI models. There is very spare code examples and documentation without some guidance is difficult. Can anyone point me in the right direction. Note the older NationalInstrument.VISANS driver will no longer compile or work under VS2019.
If you have any example code that would be cool.
|
|
|
|
|
|
|
This is why I don't use the NI NET assemblies: the good old Visa32.dll (C/C++ style, not NET) didn't change since probably 20 years and does not depend on the VS version because it binds at runtime only (NB. see answer to your question about my article/software). It is less user friendly than IVI though, I admit.
modified 1-Jul-20 0:56am.
|
|
|
|
|
For the next C# version I would like the ability to have multiple classes with the same name.
But, this is for POCO's
What do I mean?
I have an poco with the name CandidateFunction
public class CandidateFunction
{
public int CandidateFunctionId { get; set; }
public int CandidateId { get; set; }
public int FunctionId { get; set; }
public string Skills { get; set; }
public DateTime Date { get; set; }
}
As you can see, the property with the name Skills is a string.
It can be a long string and it stores one or more Skills.
But customers want to see Skills as a list so I have another POCO
public class CandidateFunctionWithSkillsList
{
public int CandidateFunctionId { get; set; }
public int CandidateId { get; set; }
public int FunctionId { get; set; }
public List<Skill> Skills { get; set; }
public DateTime Date { get; set; }
}
It has another name.
Now, I want both POCO's with the same name. I know there alternatives for what I want but I don't like them.
So, when I instantiate a new CandidateFunction, I want a popup asking me which one I want to use.
Will this ever happen and if not, why not?
|
|
|
|
|
Wiep Corbier wrote: Will this ever happen and if not, why not?
No, because you create a instances of your class at run-time, and you don't want the user to be presented with a pop-up when your application is running asking them to pick which class to instantiate.
You can already have different classes with the same name if they're in different namespaces.
You can have different classes with the same name and namespace if they're in different assemblies, with no reference between the two. But they're an absolute pain to use. You have to create an "external alias" for each reference in order to disambiguate them.
Computers aren't magic. They can't "guess" which class you meant to use. And if two classes have the same name in the same namespace, with no "external alias" to disambiguate them, then there is no way for the computer to uniquely represent the class you want to use.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Quote: Computers aren't magic. They can't "guess" which class you meant to use.
But anything else computers can guess?
It's not up to me to solve this problem. That's up to the people that invent new possibilities for c#.
I think it can happen but no one has ever ask this option.
So, get things done, make it happen.
|
|
|
|
|
But that's the point - it's not a problem. Nobody is seriously asking for the ability to have multiple classes with the same name in the same namespace in the same assembly. It just makes no sense.
Imagine everyone in your office was called Wiep. If you put a message up on the wall saying "Wiep, call me urgently! - Wiep", would anyone have the first clue who was supposed to call whom? It would just be utter chaos. You'd have to find some way to disambiguate your names in order to work.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
The point is Richard, I understand the problems.
But imagine this. You work in a office room with a colleguea also called Richard.
A person walkes in and asks for Richard. People will ask, which Richard? (popup intelli)
Got it?
I know it doesn't work yet, and I know why.
I just want it to work in the future.
ps. C# (8?) now has the ability to work around null values in classes.
Who asked for that? I didn't.
Who cares? No one.
But it's there anyway.
Just....progress
|
|
|
|
|
Wiep Corbier wrote: People will ask, which Richard?
There has to be some way to disambiguate the two people. Usually you would use the surname.
It's no different with classes. If you have two classes with the same name, then they have to be in different namespaces so that you can disambiguate them.
Otherwise, even if you could come up with an intellisense popup to let you pick a class - which could be difficult if the only difference is one character on line 42! - there would be no way to represent that choice in the compiled program.
And before you suggest that the compiler could generate some form of "mangled" name to uniquely identify the class, remember that you'd also have to represent that choice in the source code. And the source code is meant to be read by humans. There are enough arguments against using var when the type isn't clear, without making an explicit variable type ambiguous as well!
Wiep Corbier wrote: C# (8?) now has the ability to work around null values in classes.
Who asked for that? I didn't.
Who cares? No one.
Lots of people asked for it, and lots of people care. You can probably find the history in the C# Language Design repository:
GitHub - dotnet/csharplang: The official repo for the design of the C# programming language[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: There has to be some way to disambiguate the two people. Usually you would use the surname. Doesn't always work. In my family history, there is a case of three boys, born of the same parents, two of them in the same year, all with the same name. The first one was born shortly after New Year, but lived for only a few days - long enough to be baptized. The second one was born in December the same year, and lived for about a year. The third one, born a couple of years later, grew up to an adult.
When I looked up the name of their paternal grandfather, I was not very surprised: The three boys were named after him. In those days, having a heir to carry your name on, one who grew up to carry the lineage on, was essential.
But I digress.
|
|
|
|
|
It would still be very confusing if they had all made it to a family gathering. You'd have to resort to pointing at the person you meant.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
For two of them it would be pointing towards the graveyard. They never lived at the same time.
|
|
|
|
|
Richard, I know that what I want isn't possible.
That's the reason why I post this.
It is a nice to have for me and probably someone else.
But what about something else like it: inheritance.
Why?
Are people to lazy to copy properties?
inheritance is not a must. It is a nice to have.
btw: I did predict you would mention the surname
Richard?
Which Richard?
Richard String or Richard List?
ps: just for fun.
|
|
|
|
|
Inheritance is a useful tool for encapsulating state and/or behaviour. It can be overused, but at the moment in C# it doesn't introduce any contradictions or ambiguity. C# doesn't have multiple inheritance, so it doesn't suffer from the "diamond problem"[^].
I'm still struggling to see what problem you think your idea would solve. All I can see is code where I can't tell whether you meant this FooBar.Baz class, or that FooBar.Baz class, because they both have identical names.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Wiep Corbier wrote: It is a nice to have for me and probably someone else. Not really. Even if the feature were there, when you went back a day later and looked at your code you'd have no idea which class the code was referring to.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Richard Deeming wrote: There has to be some way to disambiguate the two people. I think the OP is asking for the computer to do that. In other words I write
SomeClass temp = new SomeClass();
and up pops some intellisense so that I can pick SomeClass from file1 or SomeClass from file2. Internally, C# would clearly have to track which one it is. Which makes this idea even worse because if you come back the next day to read your code you won't know which one it actually refers to.
It could be done, and probably not too hard with some sort of internal tracker in VS but a terrible idea regardless.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Wiep Corbier wrote: A person walkes in and asks for Richard. People will ask, which Richard? (popup intelli)
OK, how is the compiler going to "popup a dialog" to ask which version of the class you want when you try to "new it up"?
CandidateFunction newCandidate = new CandidateFunction()
WHICH "CandidateFunction" does the code "new up"? How is it going to know? There's no way for the compiler or the code to generate a prompt to ask. Even if it was possible, the UI type of the app comes into play. Is this a Console app? Windows Forms? WPF? ASP.NET? Each handles prompting the user is different ways, so how is the compiler to know which to use?
Oh, and how about apps with no user interface at all, like Window Services and WebAPI? How is that prompt supposed to show up?
This would have to be prompted for at compile-time. There's absolutely no way to do this at run-time.
On top of that, how is the compiler to know which type of object it is when you go to use that object elsewhere?
Did you really think about this before posting? If you want this, then it's your skill set that needs to be improved, not the compiler.
|
|
|
|