C# syntax shorthand for multiple condition testing [duplicate] - c#

This question already has answers here:
Test for multiple values in an if statement in C# [duplicate]
(5 answers)
Closed 6 years ago.
What syntax exists in C# to help with multiple condition testing?
I often have to test for multiple conditions in the following manner:
if (a == 3 || a == 4)
Perhaps I'm being subjective, but that isn't very pretty.
If there is a larger set of conditions, I could do:
if (new int[]{3, 4, 5, 6, 7}.Contains(a))
But if there are just a few conditions to test, I'm not saving any keystrokes.
Is there a syntax shortcut in the C# language that would allow me to accomplish something like the following, without many keystrokes and without extension methods, etc?
// doesn't work
if (a == 3 || 4)
This is different from this SO thread, because it is dealing with short conditions (2 or 3), all with an unchanging "a" value in an "a compare to b" comparison.

No there is not. Sorry, but this is your answer.

Related

Evaluate the string as math expression [duplicate]

This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Is there a string math evaluator in .NET?
(18 answers)
Closed 4 years ago.
I have one scenario in C#, which I fetch the expression from database, I need to parse and evaluate the same
The expression can be of any type (With + & - Operator only) as mentioned below
X+Y-Z
X-Y-Z
Z-Y+Z
Where as at run time I have any of above expression as string item and the values for each variable defined in it.
Now I need to automate the same, so that my code at run time will able to parse and evaluate the same.
I believe Switch case and if/else loop is the one way, but any can please suggest some other better and efficient way.
Thanks in advance

C# - Random.Next(Length-1) vs OrderBy(x=>Guid.NewGuid()).First() [duplicate]

This question already has answers here:
Is using Random and OrderBy a good shuffle algorithm? [closed]
(13 answers)
Are GUIDs timely ordered ? If ORDER BY used with a GUID variable type, will records created lately come late?
(3 answers)
Closed 5 years ago.
Which one is faster?
Im doing tests on a ConcurrentDictionary and want to randomize it and just return 1 result. Essentially pick a random result from it.
Which method is faster/more efficient?
Efficiency as in, as little cpu/memory while having low possible errors like conflicts and such.
Method A.
Random rand = new Random();
var result = concDict.ElementAt(rand.Next(concDict.Count() - 1));
Method B.
var result = concDict.OrderBy(x=>Guid.NewGuid()).First();
In my "vague" testing I dont see much difference apart from Method B being more efficient. Method A can succum to the concDict's Count being out of sync with the concDict.ElementAt causing ArgumentOutOfRangeException: 'maxValue' must be greater than zero. while Method B literally cant cause that.

Dynamic String Validation [duplicate]

This question already has answers here:
Serializing and Deserializing Expression Trees in C#
(7 answers)
Is it possible to dynamically compile and execute C# code fragments?
(7 answers)
Closed 7 years ago.
I wonder if it is possible to convert text to C# code for dynamic validation. For example, I'd love to be able to store a string in my DB like this:
((Value.StartsWith("CR") || Value.StartsWith("TRC")) && Value.Length == 10)
Then - pull it from the DB and use it to validate data. If would automatically swap out Value for the piece of data I'm trying to validate.
All validations would return a boolean that I would use in the code to see if the data being tested passed or failed.
I would have numerous variations of validations similar to the one above like this stored in my DB.
I've tried researching various phrases on Google but can't seem to think of what this might be called?

counting occurances of item in IEnumerable using linq in C# [duplicate]

This question already has answers here:
Find character with most occurrences in string?
(12 answers)
Closed 7 years ago.
I'm trying to find a clever way using linq to examine an IEnumerable and find the max occurrences of some element.
"aba".SomeLinqExpression(); // => 'a'
or
(new List<int>{1, 2, 3, 4, 1, 2, 1}).SomLinqExpression(); // => 1
Is there an easy built in way to do this I think I need an aggregation query but Aggregate and Count don't seem to do it. Maybe groupby?
EDIT: Clarification
I'm looking to get access to the most often seen value. I don't care how ties are handled.
if we have a string
"abcda" // note that there are 2 'a' characters.
Since 'a' is the most common thing in the sequence I want this operation to return 'a'
"abcda".SomeLinqExpression(); // returns 'a'
Well, I guess this will work:
"aqejqkndaasaamd".GroupBy(x => x).OrderByDescending(g => g.Count()).First().Key

What does the [] represent in C# [duplicate]

This question already has answers here:
What are attributes in .NET?
(11 answers)
Closed 9 years ago.
i've been developing in c# for 4 month and I still dont know what does the [] means in entity framework.
Here an example
[Column("mycolumn")]
public int Column {get;set;}
What is it functionality?
Its there another situation that i have to use it or just with entity framework?
Square brackets [ & ] mean a few different things in C#, but in this case they are saying that "Column" is an Attribute. An attribute is basically design time information that you add to classes or properties for various reasons. You can also make your own ;)

Categories