Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I'm attempting to go around a loop and append items to a StringBuilder.
However, I'm unable to do anything within the loop, and I don't think my loop is even being accessed.
Below is the code:
string initialString = "PRINT OUT PRODUCTS BELOW!\n";
StringBuilder stringBuilder = new StringBuilder(initialString);
for (int i = 0; i < products.Count(); i++)
{
stringBuilder.Append(products.ElementAt(i));
stringBuilder.Append("24");
stringBuilder.Append("36");
}
stringBuilder.Append("45");
return stringBuilder.ToString();
The final "45" will print, but the 24 or 36 will not.
If you don't see 24 and 36 then your products list is empty. There is nothing wrong with your code.
BTW you can build same string with String.Join(string separator, IEnumerable values):
String.Join("2436", products) + "45"
I don't think my loop is even being accessed
Use the debugger.
In general you should avoid ElementAt on a query that is not a collection because it always needs to execute in in the loop to get to the index. Instead use ToList first or better foreach:
foreach(var product in products)
{
stringBuilder.Append(product.ToString());
stringBuilder.Append("24");
stringBuilder.Append("36");
}
two possible reasons
1) Your product.Count is 0
2) Or you disturb the product list during loop on which loop is being run
please don't use ElementAt this can disturb your loop so I will suggest you to use foreach loop
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 3 years ago.
Improve this question
I'm having a following out of memory exception when I'm looping through a variable. My solution would be if this variable count is great than 100 only process my for loop as a batch of 100.. I'm just not sure how to do this. Any input?
var statementResults = GetCustomerDetailsForPrinting(searchCriteria.CustomerNumbers);
if statementResults.Count() > 100 then process in batch....
for (int i = 0; i < statementResults.Count(); i++)
{
}
It's not the loop that generates the out of memory exception. It's because the statementResults is an IEnumerable<T> or IQueryable<T> and is 'executed' when iterated. For example calling a Count()
Everytime you execute the Count() method, the whole query is executed (again)
Don't use a for(int i=0;...)
Do like this:
var statementResults = GetCustomerDetailsForPrinting(searchCriteria.CustomerNumbers);
foreach(var statementResult in statementResults)
{
// handle items.
}
This is how you stream the results. And if you only want to process 100 items. use:
var statementResults = GetCustomerDetailsForPrinting(searchCriteria.CustomerNumbers);
foreach(var statementResult in statementResults.Take(100))
{
// handle items.
}
You can also put the Take(100) behind the GetCustomerDetailsForPrinter() etc.
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 5 years ago.
Improve this question
I've always used Contains method to gain SQL IN functionality to my LINQ queries.
But this time, very strange thing occured.
I've got an array of string like this :
string[] FilenamesToParse = {"JOHN_X200-", "DOE_X300-", "FOO_X300_M-"};
Then I've used this array just like below:
var result = (from dps in appProcessList
where FilenamesToParse.Contains(dps.FileName)
select dps.Devices).ToList()
Above query resulted with 0 result but I'm sure that there are filenames contains words defined in FilenamesToParse array.
So I've tried below snippet and Contains worked.
foreach (var applicationProcess in appProcessList)
{
if (applicationProcess.FileName.Contains(FilenamesToParse[0]))
{
}
}
Where am I wrong here ?
Thanks in advance.
Your two appraoches aren´t similar. In your linq you´re iterating your FilenamesToParse-array and check if any of its elements exactly matches dps.FileName, whereby in the second one you iterate appProcessList and check if its FileName-property contains the first FilenamesToParse.
The following would be the linq-approach similar to your loop:
var result = (from dps in appProcessList
where FilenamesToParse.Any(x => dps.FileName.Contains(x))
select dps.Devices).ToList()
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 heard recently from some of my friends that he managed to insert a while loop inside a for. And I by "inside" I mean literally inside. I tried it in C/C++. Something like this:
for( exp1; while(); exp2)
{
exp3;
}
or
for(exp1; exp2; while())
{
exp3;
}
I've tried both ways but I don't seem to manage to make it work. Did anyone else tried this? Can someone show me an example that works?
The two programs are not valid in C.
for loops clauses in C have to be expressions (or a declaration for the first clause). A statement, for example a while statement, is not allowed.
for( int a = 17; 1 != [&](){while(!(a%2)){a/=2;};return a;}(); a=a*3+1){
std::cout <<a<<"\n";
}
is a while inside a for.
The for statement in its general form (excluding the range-based for) in fact consists from three expressions:init expression, condition expression, and one more expression. The statement while is not an expression statement so it may not be used in expressions.
I agree with the others that you cannot insert a while statement like this into for loops in C. From what I've learned the syntax for the for loop is like this:
for(initializer; condition; increment)
...
From the textbook, it says it can only take in clauses and expressions.
The while statement creates a structured loop that executes as long as the condition specified within while is true at the start of each iteration. So, I think the while condition is for the while statement itself, and not for the for loop. Cheers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want to print duplicate items using LINQ.
e.g. I want to print 1 at 10 times.
Here 1 is a string and 10 (Dynamic Number) is the number of times I want to print this string.
How can I do this?
You can use this constructor overload:
int count = 10;
string s = new String('1', count);
If you really wanted to use Linq, you could use Enumerable.Repeat:
int copies = 10;
foreach(var s in Enumerable.Repeat("1", copies))
{
Console.WriteLine(s);
}
But for that matter, a simple for-loop would work too:
int copies = 10;
for(int i = 0; i < copies; i++)
{
Console.WriteLine("1");
}
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
when using StreamReader in C# to load a txt file into a list, i assume that using a simple "If" the string's length is over a particular length, it will add it to the list. can anyone provide C# code for this? this IS homework, but it's NOT a C# class. the instructor would gladly provide this if i asked this specifically. thx.
the txt file is a dictionary of ~280,000 words, one per line. very simple move to turn into a list, but i'm wondering about getting words at least 2 characters long.
Just use LINQ to give you a subset.
List<string> lines = File.ReadLines(filename)
.Where(l => l.Length > specifiedWordLength)
.ToList();