linq subtracts number in a list [duplicate] - c#

This question already has an answer here:
C# element-wise difference between two lists of numbers
(1 answer)
Closed 2 years ago.
How can I subtract numbers in a list in query?
var q = lists.Select(v => new LegalSuitReport
{
CargoQtystr= v.Claim.ClaimBLs.Select(qq => qq.JobBL.CargoQty).ToList(),
RecipetedQtystr = v.Claim.ClaimBLs.Select(qq => qq.JobBL.LandedQty).ToList()
diff= v.Claim.ClaimBLs.Select(qq => qq.JobBL.CargoQtystr) - v.Claim.ClaimBLs.Select(qq => qq.JobBL.LandedQty)
}).ToList().Select(qs => new LegalSuitReport()
{
CargoQty= string.Join(",", qs.CargoQtystr),
RecipetedQty = string.Join(",", qs.RecipetedQtystr)
diff = string.Join(",", qs.RecipetedQtystr)
}).ToList();
I want to subtract numbers with same indexes in these cargoqty and receiptedqty and have there difference between in diff
List<decimal> cargoqty= new List<decimal>{500,100000,150};
List<decimal> RecipetedQtystr = new List<decimal>{5,90000,15};
List<decimal> diff= new List<decimal>{495,10000,135};

From the samples, it looks like Zip() would help:
var diff=cargoqty.Zip(RecipetedQtystr,(q1,q2)=>q1-q2).ToList();
Zip combines two sequences in pairs. This particular overload applies a function to the pairs before returning the result

Related

How to get a tuple with max 2nd element from array of tuples? (C#) [duplicate]

This question already has answers here:
How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate]
(9 answers)
Closed 2 years ago.
I have an array of tuples and want to get tuple with a max second element without for cycles. I think it should be some linq statement or something similar:
var a = new Tuple<string, int>[n];
// *initializing*
Tuple<string, int> mx = a.Max(t => t.Item2);
Also, it must have O(n) complexity
If you sort by the ordering constraint, then you can get the first item.
// Populate a Sample
var a = new Tuple<string, int>[10];
for(int i = 0; i <= 9; i++)
{
var t = new Tuple<string, int>(i.ToString(), i);
a[i] = t;
}
// Get First Item with Highest Value
Console.WriteLine(a.OrderByDescending(c => c.Item2).First());

get all elements that does not exist in a c# list [duplicate]

This question already has answers here:
Check for missing number in sequence
(14 answers)
Closed 4 years ago.
I have a list of numbers:
List<int> lstActive = new List<int>{1,6,7,8,10};
I want to get the numbers that does not exit in the above list and less than 10
e.g.
private List<int> GetInactive(List<int> lstActive, int MaxValue)
{
//To Do
}
Then:
List<int> lstInactive = GetInactive(lstActive, 10)
the result should be:
{2,3,4,5,9}
How can I do this ?
Enumerable.Range(0, maxValue).Where(n => !lstActive.Contains(n))
If perf is an issue, make a hashset:
var hs = new HashSet<int>(lstActive);
Enumerable.Range(0, maxValue).Where(n => !hs.Contains(n))
Try This:
List<int> lstActive = new List<int>(new int[]{1,6,7,8,10});
Enumerable.Range(1, 10).ToList().Except(lstActive).Dump();
https://dotnetfiddle.net/hyiAhs

We are trying to concatenate two arrays [duplicate]

This question already has answers here:
What is the use of Enumerable.Zip extension method in Linq?
(9 answers)
Closed 4 years ago.
We are trying to concatenate two arrays, tried the concatenate, append to list join array without success. Is there an easy function to turn two lists {a,b,c} and {1,2,3} into {a1,b2,c3}? The arrays are multiples within CDT's and we're combining a text array with an integer array.
Try Linq Zip() which
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
string[] array1 = { "a", "b", "c" };
string[] array2 = { "1", "2", "3" };
string[] result = array1.Zip(array2, (x, y) => x + y).ToArray();
in this case it concartenates two strings (x, y) => x + y
Here I have assumed that both arrats have the same length:
List<string> result;
for(int i=0;i<array1.Length;i++)
result.Add(${array2[i]}{array1[i]});

C# Iterate through two arrays or List and put matches in a third array or list [duplicate]

This question already has answers here:
Compare List and return matches in c#
(3 answers)
Closed 6 years ago.
I have two arrays
List<int> a
List<int> b
List<int> matches
And I need to put all matches in a third (match) array so that I can print that out...
I can print out both a and b like so.
a.Sort();
label1.Text = "";
foreach (int x in a)
label1.Text += x + " , ";
a.Clear();
And so on for "b"
but how to compare the two and only take the matching integers, put them in "matching" array and print them out the same way?
You could use a linq query to get values that are in both lists...
List<int> a = new List<int> {1,2,3};
List<int> b = new List<int> {2,4,6,3};
var matches = a.Intersect(b);
// Create comma-separated string of matching values...
string output = string.Join(",", matches);

I want count uniques along with value in array? [duplicate]

This question already has answers here:
How to Count Duplicates in List with LINQ
(7 answers)
Closed 9 years ago.
I have 6 numbers in array .
string[] list = { "1", "1", "2","2","1","3" };
I want result like this. please help.
"1" = 3
"2" = 2
"3" = 1
var itemCounts = list.GroupBy(l => l)
.Select(g => new { key = g.Key, count = g.Count()});
Assuming your numbers in SearchArray >0. Here is an alternative approach
You can also write a function
1) find Max - One Loop
for( int i=0;i<searchArray.length;i++){
if (searchArray[i]>max) max=searchArray[i];
}
2) Initialize an Array[Max+1]= 0
3) Loop thru each item and increment the size in Array
for( int i=0;i<searchArray.length;i++){
Array[searchArray[i]]++;
}

Categories

Resources