Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a "Panel" model that has multiple "Panel Pages". I would like to get a list of all panels, and fill each panel with its respective "Panel Pages".
Here is my code currently (that works):
public IEnumerable<DynamicCustomPanel> GetCustomPanels()
{
var customPanels = _customPanelService.GetDynamicCustomPanels();
var dynamicCustomPanels = customPanels.ToList();
foreach (var customPanel in dynamicCustomPanels.ToList())
{
var customPanelPages = _customPanelPageService.GetCustomPanelPages(customPanel.PanelGUID.ToString());
customPanel.CustomPanelPages = customPanelPages;
}
return dynamicCustomPanels;
}
How do I do this in an minimal amount of lines?
This should work:
public IEnumerable<DynamicCustomPanel> GetCustomPanels()
{
return _customPanelService.GetDynamicCustomPanels().Select(p => {
p.CustomPanelPages = _customPanelPageService.GetCustomPanelPages(p.PanelGUID.ToString());
return p;
});
}
That's technically 3 statements (two returns and an assignment) and one block, though it's kind of an abuse of the Select() method. I might write it like this instead:
public IEnumerable<DynamicCustomPanel> GetCustomPanels()
{
foreach(var p in _customPanelService.GetDynamicCustomPanels())
{
p.CustomPanelPages = _customPanelPageService.GetCustomPanelPages(p.PanelGUID.ToString());
yield return p;
}
}
Which is... also 3 statements (counting foreach) and one block, just spaced different to use one more line of text.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am working on a codebase a student with questionable knowledge made, here I am just confused if there is any reasonable intend there or if its just unnecessary... I personally would just use the ShowNewButton field and trash the _showNewBtn, please provide an opinion.
private _showNewBtn;
public bool ShowNewButton
{
get => _showNewBtn;
set
{
bbNew.Visibility = value == false ? BarItemVisibility.Never : BarItemVisibility.Always;
_showNewBtn = value;
}
}
I see no reason (in the sample you provided) for the _showNewBtn.
You now have 2 markers which contain the visibility state of the button. At some point, this will cause problems if you aren't carefull.
Either remove the _showNewBtn completely:
public bool ShowNewButton
{
get => bbNew.Visibility == BarItemVisibility.Always;
set
{
bbNew.Visibility = value == false ? BarItemVisibility.Never : BarItemVisibility.Always;
}
}
Or justify the existance of _showNewBtn which makes my remark null and void.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a string array as follows:
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
In essence, it is broken down by id|active where for example id is say 1122 and active is true or false.
Say my id was 1123, how would I search this array to get the value of true in this case? I understand Substring needs to be used with IndexOf but not sure how to tie it together.
Little bit of LINQ and String.Split should do the trick.
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
int id = 1123;
var itemWithGivenId = stringArray
.SingleOrDefault(s => int.Parse(s.Split('|')[0]) == id);
Console.WriteLine(bool.Parse(itemWithGivenId.Split('|')[1]));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this code that gets the value of a setting:
public static BTN Btns {
get => (BTN)App.DB.GetSet(SET.Btns, 2, typeof(BTN));
set => App.DB.UpdSet(SET.Btns, (int)value);
}
How can I change this so that if the value returned from GetSet is 1, then the value returned from the get will be 1 and there will be a call to App.DB.UpdSet(SET.Btns, 2) issued to change it in the database to a 2?
Can I use {} or something like that after the => ?
Firstly, if your property value is going to change after every get, you should perhaps change it to a method call - keeps things more explicit.
Secondly, you can achieve what you want by replacing your expression bodied get with a regular get, like so:
public static BTN Btns
{
get
{
var value = (BTN)App.DB.GetSet(SET.Btns, 2, typeof(BTN));
if ((int)value == 1)
{
App.DB.UpdSet(SET.Btns, (int)value + 1);
}
return value;
}
set
{
App.DB.UpdSet(SET.Btns, (int)value);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Can anyone assist on below WPF GUI how to convert to Linq due to low performance:
foreach (Grid b in main_grid.Children)
{
foreach (Control s in b.Children)
{
if (s.GetType() == typeof(Button))
{
if (s.Tag.ToString() == message)
{
if (status == "OIRS_INUSE")
{
s.Background = Brushes.Orange;
}
else
{
s.Background = defaultBackground;
}
}
}
}
}
First, you are asking the wrong question. Linq doesn't help.
One way to speed up this loop is to reduce the workload of its bottleneck:
foreach (Grid b in main_grid.Children)
{
foreach (Control s in b.Children)
{
if (s.SomeEnumValue == SomeEnum.Value)
{
s.Background = Brushes.Orange;
}
else
{
s.Background = defaultBackground;
}
}
}
First comparison if (s.GetType() == typeof(Button)) is costly:
for 100 million calls:
typeof(Test): 2756ms
TestType (field): 1175ms
test.GetType(): 3734ms
you'll have more than 5 times slower than a simple field comparison.
Second comparison if (s.Tag.ToString() == message) and third comparison status == "OIRS_INUSE" are costly
Moreover, the second comparison contains a ToString method which has its own cost.
So get rid of all these expensive comparisons and replace them with a simple field comparison such as an enum which is cheap.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I got this class that I'm using for spin text.
public class Spinner
{
private static Random rnd = new Random();
public static string Spin(string str)
{
string regex = #"\{(.*?)\}";
return Regex.Replace(str, regex, new MatchEvaluator(WordScrambler));
}
public static string WordScrambler(Match match)
{
string[] items = match.Value.Substring(1, match.Value.Length - 2).Split('|');
return items[rnd.Next(items.Length)];
}
}
But I need it to be able to spin multi spintax text.
As example
{1|2} - {3|4}
Returns: 2 - 4
So it works.
But:
{{1|2}|{3|4}} - {{5|6}|{7|8}}
Returns: 2|4} - {5|7}
So it doesn't work if there is spintax inside spintax.
Any help? :)
Regular expressions are not good in dealing with nested structures, which means you should probably try a different approach.
In your example, {{1|2}|{3|4}} - {{5|6}|{7|8}} is the same as {1|2|3|4} - {5|6|7|8}, so maybe you don't need nested spintax.