LINQ concatenating elements in two string arrays [duplicate] - c#

This question already has answers here:
How can combine two enumerations with a custom function?
(4 answers)
Closed 9 years ago.
I have two string arrays
Array1 = {"Paul","John","Mary"}
Array2 = {"12","13","15"}
I would like to know whether it is possible to join these arrays so that the resultant arrays have something like
{"Paul12","John13","Mary15"}

var Array3 = Array1.Zip(Array2, (a, b) => a + b).ToList();

You can use a Zip.
var array1 = new[] {"Paul", "John", "Mary"};
var array2 = new[] {"12", "13", "15"};
var result = array1.Zip(array2, (a1, a2) => String.Concat(a1, a2)).ToArray();

Related

linq subtracts number in a list [duplicate]

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

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

Flatten jagged array in C# [duplicate]

This question already has answers here:
Convert 2 dimensional array
(4 answers)
Closed 7 years ago.
Is there an elegant way to flatten a 2D array in C# (using Linq or not)?
E.g. suppose
var my2dArray = new int[][] {
new int[] {1,2,3},
new int[] {4,5,6}
};
I want to call something like
my2dArray.flatten()
which would yield
{1,2,3,4,5,6}
Any ideas?
You can use SelectMany
var flat = my2dArray.SelectMany(a => a).ToArray();
This will work with a jagged array like in your example, but not with a 2D array like
var my2dArray = new [,] { { 1, 2, 3 }, { 1, 2, 3 } };
But in that case you can iterate the values like this
foreach(var item in my2dArray)
Console.WriteLine(item);

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