How do I use LINQ in this scenario? [closed] - c#

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 8 years ago.
Improve this question
I am new to LINQ.
I have a SortedDictionary
SortedDictionary<int, int> book = new SortedDictionary<int, int>();
and the pair are
(5,10),(7,20),(8,30),(9,15),(10,60)
Is there any LINQ statement that can allow me to selected the lowest key whose value is more than 25? For this example, it is the key is 8

As it's a sorted dictionary (and therefore the first key matching the criteria is always the lowest key matching the criteria), you can just do:
var result = book.First(o => o.Value > 25).Key;

book.Where(i => i.Item > 25).Min(i => i.Key)

Related

Retrieve List<Object> to List<String> [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
Currently, I have a List<object> like this: List<USER>.
In User class, I have column named UserName.
I want to retrieve this column to List<string>.
How can it work without using foreach for List<USER>?
Thanks.
You can use LINQ:
List<string> userNames = users.Select(u => u.UserName).ToList();
In addition to Tim Schmelter post:
For casting IEnumerable<object> to IEnumerable<USER> you may use Cast<T> or OfType<T> method.
This can be written as follows:
var userNames = users.Cast<USER>.Select(u => u.UserName).ToList();

LINQ Split list into lists by date [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 list of objects List<Payment>. I would like to get list of lists grouped by PaymentDate. Something like that: List<List<Payments>> where each list of the list have same date (without hours).
DateTime has a Date property:
List<List<Payments>> result = payments
.GroupBy(p => p.PaymentDate.Date)
.Select(g => g.ToList())
.ToList();
You can either use GroupBy as suggested by others, or you can just create a Lookup:
var lookup = payments.ToLookup(payment => payment.PaymentDate.Date);
You can iterate over that (using each group's key as the date) or fetch all the payments for a given date using the indexer. I usually find that a lookup is cleaner than using a nested collection such as a List<List<...>> or a Dictionary<..., List<...>>.
You can do this using GroupBy(). The result will be of type List<List<Payment>>.
var result = payments.GroupBy(payment => payment.PaymentDate.Date)
.Select(group => group.ToList())
.ToList()

Need to convert SQL to Linq-To-SQL [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 8 years ago.
Improve this question
select cast(' ' as varchar(1000)) as myfield, otherfield
from tab1;
I need to convert above SQL to Linq-To-Sql.
Maybe:
from item in dataContext.Items
select new { MyField = "", Item = item };
Unfortunately, AMAIK, there is no way to declare * in projection in LINQ. Thus to have all other fields alongside your MyField field, you should write them all.

How do I find the total of all the elements in an int array c# [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 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
As it says in the title, is there a code to find out the total of all the numbers in an int array element.
E.G
int [] Football = new int [5];
And say all the elements already had values, how would i find the total of them or reference the total of them so i could add it to a variable?
That is quite easy using LINQ:
Football.Sum();
You need to iterate over the elements. This is basic stuff and should be covered in any C# tutorial:
int total = 0;
foreach(int i in Football) {
total += i;
}
Or you can use LINQ:
int total = Football.Sum();

How to search through all items of a combobox in 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 a combobox, and I would like to search through every element in it.
How can I do this? (also the number of items is not the same everytime, but this is not so important).
I am using c# windows form application.
you can do this
for (int i = 0; i < myComboBox.Items.Count; i++)
{
string value = myComboBox.GetItemText(myComboBox.Items[i]);
}
Use a foreach loop. It will iterate all your items of ComboBox regardless of their count, e.g.
foreach(var item in myComboBox.Items)
{
// do something with your item
}

Categories

Resources