Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
List<Object> rollerliste = (from row in roller.AsEnumerable() select (row["rolName"])).ToList();
List<Object> yetkiliste = (from row in roller.AsEnumerable() select (row["Visible"])).ToList();
for(int r = 0; r < rollerliste.Count(); r++)
{
for (int y = 0; y < yetkiliste.Count(); y++)
{
if(rollerliste[r].ToString() == "frmMasalar" && yetkiliste[y].ToString() == "true" && r == y)
{
cu.frmMasalar = 1;
}
else
{
cu.frmMasalar = 0;
}
}
}
Actually
if(rollerliste[r].ToString() == "frmMasalar" && yetkiliste[y].ToString() == "true" && r == y)
it seems to be checking for correct data but not working.
rollerliste
yetkiliste
frmMasalar
True
frmYonetim
True
I just want to make check rollerliste if column1 is true "button.Enable = true" or false
Please note that, within your loops, you are overwriting cu.frmMasalar over and over again. That alone might be the reason you're not getting what you want.
I'm not sure I totally understand what you want to do. But, check whether this might be simpler:
cu.frmMasalar = 0;
foreach(var row in roller.AsEnumerable()) {
if((string) row["rolName"] == "frmMasalar" && (bool) row["Visible"]) {
cu.frmMasalar = 1;
break;
}
}
There are also more condensed ways to do this, if the point is to find the one entry where rolName == "frmMasalar":
cu.frmMasalar = 0;
var matchingRow = roller.AsEnumerable()
.FirstOrDefault(r => (string) r["rolName"] == "frmMasalar"
&& (bool) row["Visible"]);
if(matchingRow != null)
cu.frmMasalar = 1;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have an Infint class with one List field. In the main program, I want to read numbers (character by character) and add them to this list.
The assignment itself doesn't matter in this moment, what I'm wondering is why I can't add values to the list of the Infint object from the if/else block (inside the first while loop). I have initialised the List itself in the class constructor. I added the line number1.Number.Add(1) for debugging reasons, since the code wasn't working and I've come to realize it seems to be working fine outside that if/else block. I can't seem to find the solution.
Here are the following implementations:
internal class Infint
{
public bool isNegative { get; set; }
public List<int> Number { get; set; }
public Infint()
{
isNegative = false;
Number = new List<int>();
}
}
static void Main(string[] args)
{
using (StreamReader sr = new StreamReader("infint.txt"))
{
int i;
char num;
for (int operation = 0; operation < 3; operation++)
{
Infint number1 = new Infint();
Infint number2 = new Infint();
number1.Number.Add(1); //works
i = sr.Read();
while (sr.Peek() >= 0)
{
number1.Number.Add(1); //works
if (i == 10 || i == 13)
break;
num = (char)i;
if (operation == 0)
{
number1.Number.Add(1); //doesn't work
if (i == 45)
number1.isNegative = true;
else
{
number1.Number.Add(1); //doesn't work
//number1.Number.Add((int)Char.GetNumericValue(num));
}
}else if(operation == 1)
{
if (i == 45)
number2.isNegative = true;
else
{
number2.Number.Add((int)Char.GetNumericValue(num));
}
}else if(operation == 2)
{
Console.WriteLine("counts of num1: " + number1.Number.Count);
for (var k = 0; k < number1.Number.Count; k++)
{
Console.Write(number1.Number[k]);
}
Console.WriteLine("counts of num2: " + number2.Number.Count);
for (var k = 0; k < number2.Number.Count; k++)
{
Console.Write(number2.Number[k]);
}
}
i = sr.Read();
}
if (i == -1)
{
break;
}
//i = sr.Read();
}
}
Console.Read();
}
Removing the various noise in your code (it's extremely low quality, if you care), what you're left with is:
using var sr = new StreamReader("infint.txt");
for (int operation = 0; operation < 3; operation++)
{
var i = sr.Read();
while (sr.Peek() >= 0)
{
if (i == 10 || i == 13) break;
if (operation == 0)
{
// oh noes
}
}
}
So looking at the code and assuming what you describe is correct (if vague), that means that your text file starts with a new line, possibly two (you don't even mention your OS, let alone the contents of the file). If that is indeed the case, then it should be strikingly obvious why operation can't be 0 around the branch you're expecting it to be.
I have a method that iterate through a list and display the result of each type one followed by another meaning that I will display result from rd followed by result from cv followed by adz. But the actual method is very slow and it takes long time to retrieve back the results. what are the ways to improve the performance and will it matter using different data structure
private List<AllJobModel> GetAllJobModelsOrder(List<AllJobModel> result)
{
var countItems = result.Count;
List<AllJobModel> list = new List<AllJobModel>();
while (countItems != 0)
{
for (int i = 0; i < countItems; i++)
{
if (result.ElementAt(i).JobImage.Contains("rd"))
{
list.Add(result.ElementAt(i));
result.RemoveAt(i);
countItems--;
break;
}
}
for (int i = 0; i < countItems; i++)
{
if (result.ElementAt(i).JobImage.Contains("cv"))
{
list.Add(result.ElementAt(i));
result.RemoveAt(i);
countItems--;
break;
}
}
for (int i = 0; i < countItems; i++)
{
if (result.ElementAt(i).JobImage.Contains("adz"))
{
list.Add(result.ElementAt(i));
result.RemoveAt(i);
countItems--;
break;
}
}
for(int i =0; i < countItems; i++)
{
if((!result.ElementAt(i).JobImage.StartsWith("rd") && !result.ElementAt(i).JobImage.StartsWith("adz")) && !result.ElementAt(i).JobImage.StartsWith("cv"))
{
list.Add(result.ElementAt(i));
result.RemoveAt(i);
countItems--;
break;
}
}
}
return list;
}
This is a bad way to sort. You're effectively looping over the list 4 times, extracting specific groups of items each time. You also have a potential bug by removing items as you are iterating over the list. While there are many improvements you can make to your method, a better way would be to use OrderBy with the following sort condition:
list = result.OrderBy( m => m.JobImage.Contains("rd") ? 1 :
m.JobImage.Contains("cv") ? 2 :
m.JobImage.Contains("adz") ? 3 :
4)
.ToList();
Check out this link: https://cc.davelozinski.com/c-sharp/fastest-collection-for-string-lookups
Was very helpfull for me.
Regarding your specific problem. It looks like you're doing some filtering of the data. Linq has an easy way of doing that. I haven't test performance but if you can give me some example data ill try it for you. It could be something like this.
private List<AllJobModel> GetAllJobModelsOrder(List<AllJobModel> result)
{
return result.Where(x => x.JobImage.Contains("rd") || x.JobImage.Contains("cv") || x.JobImage.Contains("adz")).ToList();
}
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 4 years ago.
Improve this question
if (resourceinfo != null && resourceinfo.Products != null)
{
foreach (var product in resourceinfo.Products)
{
if (product.relatedEntities != null)
{
for (int i = 0; i < product.relatedEntities.Length; i++)
{
if (product.relatedEntities[i].reference.Equals("CONT1234"))
{
if (product.resources != null)
{
foreach (var item in product.resources)
{
if (item.resource != null && item.resource.resourceCharacteristics != null)
{
for (int j = 0; j < item.resource.resourceCharacteristics.Length; j++)
{
var ele = item.resource.resourceCharacteristics;
if (ele[j].name.ToLower().Contains(IMEI_VALUE_NAME))
{
imeiNo = respObj.resourceCharacteristics[0].value = ele[j].value;
break;
}
}
}
break;
}
}
}
}
}
break;
}
}
I want this to be in lambda expression or linq query format. When I try to bind into lambda expression whenever there is null or empty then an error shows up:
instance of an object is not defined
your question is not super clear, but the best query I could get is this:
var query =
(
from product in resourceinfo?.Products ?? Enumerable.Empty<Product>().Take(1)
from relatedEntity in product?.relatedEntities
where relatedEntity.reference.Equals("CONT1234")
from item in product?.resources.Take(1)
from ele in item?.resource?.resourceCharacteristics
where ele.name.ToLower().Contains(IMEI_VALUE_NAME)
select ele.value
).Take(1);
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Following is my code,where I need to continue the loop in case of exception inside try block.
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
try
{
if (name != "")
{
name=name.ToString().Substring(12);
break;
}
}
catch{
continue;
}
}
Please tell me if i`m wrong at any place in my code.Please check for performance wise too.
Thanks in advance.
The continue is unneeded. It will automatically continue.
var name = doc.FirstOrDefault(x => !string.IsNullOrEmpty(x) && x.Length >= 12);
You don't need exception handling if you can avoid it:
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
if(name != null && name.Length >= 12)
{
name = name.Substring(12);
break;
}
}
Never use exceptions for something that is not exceptional. If you don't expect any of documents will have length less that 12, then you can use exceptions (but also not just for control flow):
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
if (name.Length < 12)
throw new FooException("Wrong document found!");
// do something with name
}
You do not need break or continue here.
try this.
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
try
{
if (!string.IsNotNullOrEmpty(name))
{
name=name.ToString().Substring(12);
}
}
catch{ }
}
I have a function that retrieves a list of device names and stores then in a variable. Then the next step is to get info using 1 device name per line and keep going till the loop is complete.
String text = "";
String errors = "";
for (int i = 0; i < collection.Result.Count; i++)
{
deviceNames += collection.Result[i].DeviceName + Environment.NewLine;
getvirtuals.Location = deviceNames;
var virtuals = client.GetKnownVirtuals(getvirtuals, LtmKeyType.VirtualAddressPort);
if (virtuals.Result == null)
{
i++;
getvirtuals.Location = deviceNames;
for (int v = 0; v < virtuals.Result.Count; v++)
{
try
{
LtmKey virtualKey = new LtmKey();
virtualKey.Location = virtuals.Result[v].Location;
virtualKey.LocationType = virtuals.Result[v].LocationType;
virtualKey.Key = virtuals.Result[v].Key;
virtualKey.KeyType = LtmKeyType.VirtualAddressPort;
virtualKey.AdminGroup = admingroupComboBox.Text;
var memberStatus = client.GetMemberStatus(virtualKey);
for (int j = 0; j < memberStatus.Result.Count; j++)
{
VirtualMemberStatus status = memberStatus.Result[j];
text += String.Format("{5},{4},{0},{1},{2},{3}" + Environment.NewLine, status.Member.Address, status.Member.Port, status.EffectiveStatus, status.DesiredStatus, virtualKey.Key.Replace(":", ","), DateTime.UtcNow);
toolStripProgressBar1.PerformStep();
}
}
catch
{
errors += String.Format("{0} Error Code: 2, Error occurred, check device name (case senstive) and admin group. This error may also occur due to connection loss, try again." + Environment.NewLine, DateTime.UtcNow);
}
}
this.allResultsBox.Text = text;
getallstatusButton.Enabled = true;
}
}
The problem that I am running into is that if virtuals is null the tool crashes, instead what I want to do is if virtuals = null I want to move onto the next item from the list. I have tried a if statement but it is not working the way planned, it still comes back as null.
Well this seems like a problem to start with:
if (virtuals.Result == null)
{
i++;
getvirtuals.Location = deviceNames;
for (int v = 0; v < virtuals.Result.Count; v++)
...
If virtuals.Result is null, how do you expect virtuals.Result.Count to work? I suspect you meant:
if (virtuals.Result != null)
However, I suspect you really just want:
// Keep going with the next iteration of the for loop
if (virtuals == null || virtuals.Results == null)
{
continue;
}
If all you want is to go to the next loop iteration if virtuals is null then you want
if (virtuals == null) continue;
How about just inserting:
if(virtuals == null)
continue;
right after the line
var virtuals = client.GetKnownVirtuals(getvirtuals, LtmKeyType.VirtualAddressPort);
Have you tried changing the line:
if (virtuals.Result == null)
to:
if ((virtuals != null) && (virtuals.Result != null))
If this doesn't solve your issue, then you need to indicate what the additional errors are.
if (virtuals.Result == null)
make this
if (virtuals == null)