Creating a dynamic list of arrays with LINQ [closed] - c#

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();

Related

foreach loop with 2 hashset variables c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have the following
variableAs = "A12,B12,C12"
variableBs = "1.54,75.30,55.50"
method (HashSet<string> variableAs, HashSet<double> variableBs)
foreach (var variableA in variableAs)
{
Method here requires the two values, must have preserved datatype
and be in same order, ie A12 with 1.54, B12 with 75.30
}
I have tried zip from this answer but I do not know how it works with hashset arrays,
NOTE the Method i am editing has it has hashset, the actual values are for example only, if there is an error, It must be my understanding of what a hashset is but I cannot change the hashset
You need to first populate your input-strings into some collection:
var As = variableAs.Split(',');
var Bs = variableBs.Split(',');
However a HashSet is not an ordered collection and thus the wrong choice here. You'd need a List or just an array.
Now you may use the mentioned Zip-function to combine the two elements together:
var result = As.Zip(Bs, (a, b) => new { a, b });
See the complete code at this fiddle: https://dotnetfiddle.net/9qTG2E

How to map IEnumerable<string> convert to string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Method that accepts two Dal objects =>>
Method(DalFirst[] source1, DalSecond[] source2):
source1.Select(
src => new DataExmaple()
{
Id = src.Id,
...
AdditionalField = source2.Select(x => x.Field).ToString()
}
I get the output name as just type
System.Linq.Enumerable+SelectArrayIterator`2[....
With FirstOfDefault => it turns out, but the same values are everywhere.
There is no default string representation of a collection. You'd need to specify how you want the data to render as a string. For example, if you want each value separated by a comma (or any other delimiter) then you can join the collection values with String.Join:
AdditionalField = String.Join(",", source2.Select(x => x.Field))
The problem is this expression:
AdditionalField = source2.Select(x => x.Field).ToString()
At this point, source2 is still the entire DalSecond[] source2 array. You need to do something to map each source1 item to the specific matching source2 value(s). I could give you more, but there's not enough information in the question yet to infer what you really want to do here.

using a function inside a lamba expression [closed]

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 4 years ago.
Improve this question
To manage rounding there are usually two methods, the first method is round values then to sum them. Or sum values then to round them. Of course to the required precision that you want.
I want to go with the first method and I need to update this line that currently do the opposite.
this.ClonedEntity.MontantHT = ArroundDecimal.CustomDecimalArround(correctedLines.Sum(l => l.MontantHT ?? 0));
When I try to call my static method in the lambda expression it doesn't work.
How would you suggest to do it while keeping use of the linq syntax ?
Thank you.
You could try something like this:
this.ClonedEntity.MontantHT = correctedLines
.Select(x=>ArroundDecimal.CustomDecimalArround(x.MontantHT ?? 0))
.Sum();
something = correctedLines.Sum(l => ArroundDecimal.CustomDecimalArround(l.MontantHT ?? 0))

Compare 2 text file contents in c# and remove same content [closed]

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

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()

Categories

Resources