var listA = new List<string> {"One", "Two", "Three"};
var listB = new List<int> {1, 2, 3};
var listC = new List<Tuple<string, int>>();
Here I've got two Lists with the same length. Is there a way to fill third listC with LINQ?
"One", 1
"Two", 2
"Three", 3
I think you are looking the Zip extension method:
var listA = new List<string> { "One", "Two", "Three" };
var listB = new List<int> { 1, 2, 3 };
var listC = listA.Zip(listB, (s, i) => new Tuple<string, int>(s, i)).ToList();
Related
I'd like to know how to Count the instances of the first element in a list, then the second etc. and output these values.
var SPFK_List = new List<string>() {
"one", "one", "one",
"two", "two",
"three", "three", "three"
};
Inputs.ones.Value = *(number of one's)*
Inputs.twos.Value = *(number of two's)*
Try GroupBy (Linq), e.g.:
using System.Linq;
...
var SPFK_List = new List<string>() {
"one", "one", "one",
"two", "two",
"three", "three", "three"
};
// {3, 2, 3}
int[] counts = SPFK_List
.GroupBy(item => item)
.Select(group => group.Count())
.ToArray();
Or (add Where if you want to count only some items)
// {{"one", 3}, {"two", 2}, {"three", 3}}
Dictionary<string, int> counts = SPFK_List
//.Where(item => item == "one" || item == "two")
.GroupBy(item => item)
.ToDictionary(group => group.Key, group => group.Count());
Inputs.ones.Value = counts.TryGetValue("one", out int count) ? count : 0;
A possible solution:
Inputs.ones.Value = SPFK_List.Where(x => x.Equals("one")).Count();
Simply use Count method from System.Linq with overload accepting a Func<TSource,bool> predicate
var SPFK_List = new List<string>() {
"one", "one", "one",
"two", "two",
"three", "three", "three"
};
Inputs.ones.Value = SPFK_List.Count(s => s.Equals("one", StringComparison.Ordinal));
Inputs.twos.Value = SPFK_List.Count(s => s.Equals("two", StringComparison.Ordinal));
I have two lists:
list1 = [a,b,c,4]
list2 = [1,23,5,6]
Now I need to create an anonymous object using linq lambda.
Something like.
list1 = DataTable.AsEnumerable().toList();
list2 = DataTable.AsEnumerable().toList();
var result = list1.Where(x => x.Field<int>(1) == 2018).Select(x => new[] {
new {x = "XYZ", y = x[0], z = list2[0]},
....}
}
How do I go about doing this?
You need Zip Linq method, consider this example:
int[] list1 = {1, 2, 3};
string[] list2 = {"a", "b", "c"};
var result = list1.Zip(list2, (i, s) => new {y = i, z = i});
Your code is fine, it just needs some small fixes:
string [] list1 = { "a", "b", "c", "4" };
int[] list2 = { 1, 23, 5, 6 };
object[] list3 = { "test", DateTime.Now, 56 };
var result = list1.Where(x => x == "a").Select(x =>
new { x = "XYZ", y = x[0], z = list2[0], t = list3[1] }).ToList();
i have this code for c#
var wordCounts1 = myInterface.GetWordCounts(new[] {
"one", "one", "two", "three", "three",});
the expected output should be:
{"one", 2}, {"two", 1}, {"three",2}
what should I use to get this output loop or array?
Grouping by word should work
new[] { "one", "one", "two", "three", "three" }
.GroupBy(x => x)
.Select(x => new
{
Word = x.Key,
Count = x.Count()
});
I am writing my testcode and I do not want wo write:
List<string> nameslist = new List<string>();
nameslist.Add("one");
nameslist.Add("two");
nameslist.Add("three");
I would love to write
List<string> nameslist = new List<string>({"one", "two", "three"});
However {"one", "two", "three"} is not an "IEnumerable string Collection". How can I initialise this in one line using the IEnumerable string Collection"?
var list = new List<string> { "One", "Two", "Three" };
Essentially the syntax is:
new List<Type> { Instance1, Instance2, Instance3 };
Which is translated by the compiler as
List<string> list = new List<string>();
list.Add("One");
list.Add("Two");
list.Add("Three");
Change the code to
List<string> nameslist = new List<string> {"one", "two", "three"};
or
List<string> nameslist = new List<string>(new[] {"one", "two", "three"});
Just lose the parenthesis:
var nameslist = new List<string> { "one", "two", "three" };
Posting this answer for folks wanting to initialize list with POCOs and also coz this is the first thing that pops up in search but all answers only for list of type string.
You can do this two ways one is directly setting the property by setter assignment or much cleaner by creating a constructor that takes in params and sets the properties.
class MObject {
public int Code { get; set; }
public string Org { get; set; }
}
List<MObject> theList = new List<MObject> { new MObject{ PASCode = 111, Org="Oracle" }, new MObject{ PASCode = 444, Org="MS"} };
OR by parameterized constructor
class MObject {
public MObject(int code, string org)
{
Code = code;
Org = org;
}
public int Code { get; set; }
public string Org { get; set; }
}
List<MObject> theList = new List<MObject> {new MObject( 111, "Oracle" ), new MObject(222,"SAP")};
This is one way.
List<int> list = new List<int>{ 1, 2, 3, 4, 5 };
This is another way.
List<int> list2 = new List<int>();
list2.Add(1);
list2.Add(2);
Same goes with strings.
Eg:
List<string> list3 = new List<string> { "Hello", "World" };
List<string> nameslist = new List<string> {"one", "two", "three"} ?
Remove the parentheses:
List<string> nameslist = new List<string> {"one", "two", "three"};
It depends which version of C# you're using, from version 3.0 onwards you can use...
List<string> nameslist = new List<string> { "one", "two", "three" };
I think this will work for int, long and string values.
List<int> list = new List<int>(new int[]{ 2, 3, 7 });
var animals = new List<string>() { "bird", "dog" };
Say I have two lists:
var list1 = new int[] {1, 2, 3};
var list2 = new string[] {"a", "b", "c"};
Is it possible to write a LINQ statement that will generate the following list:
var result = new []{
new {i = 1, s = "a"},
new {i = 1, s = "b"},
new {i = 1, s = "c"},
new {i = 2, s = "a"},
new {i = 2, s = "b"},
new {i = 2, s = "c"},
new {i = 3, s = "a"},
new {i = 3, s = "b"},
new {i = 3, s = "c"}
};
?
Edit: I forgot to mention I didn't want it in query syntax. Anyway, based on preetsangha's answer I've got the following:
var result = list1.SelectMany(i => list2.Select(s => new {i = i, s = s}));
var result = from l1 in list1
from l2 in list2
select new { i = l1, s = l2};
preetsangha's answer is entirely correct, but if you don't want a query expression then it's:
var result = list1.SelectMany(l1 => list2, (l1, l2) => new { i = l1, s = l2} );
(That's what the compiler compiles the query expression into - they're identical.)