Print Duplicate Items using LINQ [closed] - c#

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");
}

Related

Fill combobox according to month of days value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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.
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
Improve this question
I am doing a c# project of making "Age Calculator" and I have the following problem:
When i select February from month's combo box then in date's combo box it shows 1-31 numbers. But i want to show 1-29.How can i solve this without database?
private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedValue == "February")
{
for (int i=1; i<=29; i++)
Listbox2.Items.Add(i.ToString());
}
}

C# won't loop around my loop [closed]

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

How to create a testing / temp in memory only DataTable using Linq [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
Is there any way to create a datatable with static value let say
Column : col1,col2.....coln
Rows : for n number of rows
For first row values row11,row12......row1n
for second row values row21,row22.....row2n
.
.
.
for nth row values rown1,rown2......rownn
Any stylish way to do it using lambda/linq...may be in a single line in a expertise way ..
I know it is an un-cool question ("outdated technology" - John Saunders) but I can't resist:
const int cols = 6;
const int rows = 20;
DataTable nt = new DataTable("new table");
nt.Columns
.AddRange(
Enumerable
.Range(1,cols)
.Select(x => new DataColumn("col"+x.ToString())).ToArray());
Enumerable
.Range(1,rows).ToList()
.ForEach(x => nt.Rows
.Add(
Enumerable
.Range(1,cols)
.Select(y => "row"+x.ToString()+"col"+y.ToString()).ToArray()));
Sorry about the strange formatting but I did not want a horizontal scroll bar.

Remove items from one list in another c# with high performance [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 need remove all items step by step from list and add removed items to another list, but I need max effective and hithperformance operation. What is whe best practics?
You could use this code and hope that framework'll do the best for you:
public static void MoveItems<T>(List<T> list1, List<T> list2)
{
list2.AddRange(list1);
list1.Clear();
}
list2 = list1.ToList();
list1.Clear();

Splitting a string into parts [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 would like to split a string in C# for example:
120530
so it will be like this:
Day: 12
Time: 0530
But it will be without spaces, just as it is 120530
How can I do that?
I'm assuming that you know that it will always be 6 digits with the first 2 being day and last 4 being time.
Utilize the Substring() method of your string object...
string allTogether = "120530";
string day = allTogether.Substring(0, 2);
string time = allTogether.Substring(2);
var dayAndTime = "120530";
var day = dayAndTime.Substring(0, 2);
var time = dayAndTime.Substring(2, 4);

Categories