Condensing this logic with linq [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I convert the following logic to LINQ?
Flaggedlist is a type of List<string>. request.flagged is a numeric value in a request POCO.
if (request.Flagged == 1)
{
if (!patient.UserFlaggedList.Contains(request.UserId))
{
flaggedList.Add(request.UserId);
}
}
else if (request.Flagged == 0)
{
string usrid = flaggedList.Where(a => a == request.UserId).FirstOrDefault<string>();
flaggedList.Remove(usrid);
}

It can be boiled down to two simple IF statements and the 2nd part shortened further still:
if (request.Flagged == 1 && !patient.UserFlaggedList.Contains(request.UserId))
flaggedList.Add(request.UserId);
if (request.Flagged == 0)
flaggedList.Remove(flaggedList.FirstOrDefault(a => a == request.UserId));

This is pretty condensed and clear, not sure why you'd want to make it more condensed.
If you really want to make it shorter, maybe you can do this:
if (request.Flagged == 1 && !patient.UserFlaggedList.Contains(request.UserId))
{
flaggedList.Add(request.UserId);
}
else if (request.Flagged == 0)
{
flaggedList.Remove(flaggedList.Where(a => a == request.UserId).FirstOrDefault<string>());
}

Related

trouble with if statement and the > operator [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to write an if statement and i'm having trouble with my variables. it states operator > can not be applied to type int and string. code located below. both variables are displaying a int.
if (e.CmsData.Skill.InQueueInRing > "0")
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString(); }));
callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
}
else if (e.CmsData.Skill.AgentsAvailable > "0")
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString(); }));
callsWaitingData.Foreground = new SolidColorBrush(Colors.Green);
}
else
{
callsWaitingData.Text = "0";
callsWaitingData.Foreground = new SolidColorBrush(Colors.Yellow);
}
This error couldn't really get much more descriptive.
operator > can not be applied to type int and string
if (e.CmsData.Skill.InQueueInRing > "0")
int -----^ ^--- string
Change it to
if (e.CmsData.Skill.InQueueInRing > 0)
Then both sides of the boolean logic is an int.

Convert string to generic type c# (Convert string to T) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a method that needs to convert a string to the generic type:
T GetValue<T>(string name)
{
string item = getstuff(name);
return item converted to T // ????????
}
T could be int or date.
you can use Convert.ChangeType
T GetValue<T>(string name)
{
string item = getstuff(name);
return (T)Convert.ChangeType(item, typeof(T));
}
if you need to limit input types only for int and DateTime, add condition like below
if (typeof(T) != typeof(int) && typeof(T) != typeof(DateTime))
{
// do something with other types
}

How to optimize C# code evaluating a condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
How to optimize the following C# code?
public class SampleClass
{
public bool IsGreaterThenZero(int i)
{
if (i > 0)
return true;
else
return false;
}
}
Since binary comparison operators, such as >, <, and so on, produce a bool result, ou can return i>0 directly, like this:
public bool IsGreaterThenZero(int i) {
return i > 0;
}

2D Array get number of not null rows [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm stuck in a problem.
I have a class :
public class CustomerViewModel
{
public string Name;
public string[][] values;
public bool[] flag;
}
I wish to get count of rows in values[][] where row !=null
Assuming you already have:
var vm = new CustomerViewModel();
And have populated the values array, then..
For a count of non-null rows try:
var count = vm.values.Count(i => i != null);
Or for all rows where values[row][0] is not null:
var count = vm.values.Count(i => i != null && i.Length > 0 && i[0] != null);

Take the info from json file c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have Json file in string (for example):
#{
"Url": "http://site.com/?q=windows8"
}
How can i take the information after ?q= on c# (windows 8). Sorry for my English.
You can use the querystring.
in Codebehind file
public String q
{
get
{
if (Request.QueryString["q"] == null)
return String.Empty;
return Convert.ToString(Request.QueryString["q"]);
}
}
then use the line below to get the value
var index = ('<%=q%>');
You can do simply this :
string s = "myURL/?q=windows8";
// Loop through all instances of ?q=
int i = 0;
while ((i = s.IndexOf("?q=", i)) != -1)
{
// Print out the substring. Here : windows8
Console.WriteLine(s.Substring(i));
// Increment the index.
i++;
}

Categories