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()
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 2 years ago.
Improve this question
I have something like this:
var test = teamMemberInfoList
.Select(p => new {
p.AssignedTo,
p.RemainingCapacity,
p.RemainingWork,
p.RemainingWork-
p.RemainingCapacity })
.ToArray();
I would like a list of arrays of strings from values from x numbers of properties. Is there anyway to do this? And the C# line does not work because I cannot do plus or minus either that is also a wish.
Yes you could use Tuple
test will be an array of Tupe>string>
var test = teamMemberInfoList.Select(p => (p.AssignedTo, p.RemainingCapacity,p.RemainingWork, p.RemainingWork - p.RemainingCapacity )).ToArray();
or List and test will be an array of List
var test = teamMemberInfoList.Select(p => new List<string>(){p.AssignedTo, p.RemainingCapacity,p.RemainingWork, p.RemainingWork - p.RemainingCapacity }).ToArray();
You can do plus or minus but you need to provide some name for resulting property (for others compiler will reuse the member access ones):
var test = teamMemberInfoList
.Select(p => new
{
p.AssignedTo,
p.RemainingCapacity,
p.RemainingWork,
Diff = p.RemainingWork- p.RemainingCapacity
})
.ToArray();
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)
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 have 2 text file in C# say: File A & File B. I want to compare both file contents and if there is any content found in File A which is present in File B then i want to remove that content in File B .
So if is there any method in c# which can do this for me then please let me know?
If you want to compare the lines you could use this query:
var newLines = File.ReadLines(f2Path).Except(File.ReadLines(f1Path)).ToList();
File.WriteAllLines(f2Path, newLines);
Edit: according to your comment(s)
i want to exclude all lines from file2 where the id exists in file1
You could use this left-outer-join with LINQ:
var idInF2 = from f2Line in File.ReadLines(f2Path)
join f1Line in File.ReadLines(f1Path)
on f2Line.Split(',')[0].Trim() equals f1Line.Split(',')[0].Trim() into gj
from lineOne in gj.DefaultIfEmpty()
where lineOne == null
select f2Line;
File.WriteAllLines(f2Path, idInF2.ToList());
or you could use this more readable but less efficient approach using Contains:
var f1IDs = File.ReadLines(f1Path).Select(l => l.Split(',')[0].Trim());
var newLines = File.ReadLines(f2Path)
.Select(l => new { Line = l, ID = l.Split(',')[0].Trim() })
.Where(x => !f1IDs.Contains(x.ID))
.Select(x => x.Line).ToList();
File.WriteAllLines(f2Path, newLines);
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();
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
Select * from table
where Numero_Operacion in
(
Select Numero_Operacion from table
group by Numero_Operacion
having count(Numero_Operacion)>1
)
THANKS!
It would be something like this
<Table>.GroupBy(x => x.Numero_Operacion)
.Where(x => x.Count() > 1)
.SelectMany(x => x)