I have two dictionaries:
name = ["A","B","C","D","E"]
number = ["1","2","3","4","5"]
How can I produce the following, using built-in functions?
[
{name : "A", number: "1"},
{name : "B", number: "2"},
{name : "C", number: "3"},
{name : "D", number: "4"},
{name : "E", number: "5"}
]
The desired result you have shown is not a single dictionary, but an array of dictionaries, i.e. [[String: String]].
You can convert the two arrays like this:
name = ["A","B","C","D","E"]
number = ["1","2","3","4","5"]
let result = zip(name, number).map { ["name": $0, "number": $1] }
However, I would suggest you create a struct/class for storing these name/number pairs, rather than using an array of dictionaries:
struct Foo { // give this an appropriate name!
let name: string
let number: String
}
let result = zip(name, number).map { Foo(name: $0, number; $1) }
Related
Hi I need help writing query.
doc in ES is
{ A: "A", B: ["1","2"] }
result from query should be: hits(2x)
{ A:"A" , B: "1"},
{ A:"A" , B: "2" }
is this possible?
shoppingList[4...6] = ["Bananas", "Oranges"]
Don't use subscript syntax to append a new item to an array.
But I realize append one or more new items to array with ranges successed. For example:
var shoppingList : Array<String> = ["Bread", "Milk", "Blackingbar", "Chian", "baba", "Oppo", "Xiaomi"]
shoppingList[4...6] = ["Bananas", "Oranges", "Huewei", "5", "6"]
for obj in shoppingList {
print(obj)
}
Why we have recommeded "don't use it" ????
The precise quote from the documentation is
You can’t use subscript syntax to append a new item to the end of an
array.
Your code example replaces three items at index 4 - 6 with five other items simultaneously which is something different. As far as the subscripted range is not out of bounds the operation succeeds.
Im not sure why you would want to make 5 items replace the last 3 but if you need to do it, you could do this:
var shoppingList : Array<String> = ["Bread", "Milk", "Blackingbar", "Chian", "baba", "Oppo", "Xiaomi"]
shoppingList = shoppingList[0..<4] + ["Bananas", "Oranges", "Huewei", "5", "6"]
I am trying to sort a column in SSRS by the Font Color of that column, but am unable to grasp how... The color code that I have developed works as such.
=Switch(Fields!Prk_name.Value = "Lot 1", "DarkGoldenrod",
Fields!Emp_default.Value = "L" OR Fields!Emp_default.Value = "B", "Black",
Fields!Emp_default.Value = "C" AND Fields!Perm_prk.Value = "Y", "Green",
Fields!Emp_default.Value = "C", "Purple")
What I was think of running under the SortExpression is this:
=Switch(Fields!Name.Color = "DarkGoldenrod" SortExpression (),
Fields!Name.Color = "Black" SortExpression (),
So on and so forth just don't know what to put for SortExpression? I would need a way to sort for all 4 color types the base form of this is group by color then sort ABC.
Chris Lätta's post gave me an idea I gave that a try still does not work, but it might help to narrow down what I am looking for.
=Switch(Fields!Name.Color = "Darkgoldenrod", 1,
Fields!Name.Color = "Purple", 2,
Fields!Name.Color = "Black", 3,
Fields!Name.Color = "Green", 4,
True, 5)
Just sort by a number as your first sort expression:
=Switch(Fields!Prk_name.Value = "Lot 1", 1,
Fields!Emp_default.Value = "L" OR Fields!Emp_default.Value = "B", 2,
Fields!Emp_default.Value = "C" AND Fields!Perm_prk.Value = "Y", 3,
Fields!Emp_default.Value = "C", 4,
True, 5)
then add another sort criteria to sort by the ABC column.
I need to generate a string that has a comma delimited list, but no comma after the last element.
var x = new List<string>() { "a", "b", "c" };
should yield:
a,b,c
Yes, a very simple thing to do using "normal" techniques, but I hope with linq there is a more elegant way.
var cols =context.Database.SqlQuery<String>("select Column_Name from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = {0};", prefix + table);
No need Linq, just use String.Join
String.Join(",", new List<string>() { "a", "b", "c" });
String class provide Join method to join string array with delimiter.
Code:
var x = new List<string>() { "a", "b", "c" };
String.Join(",",x.ToArray());
documentation: http://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx
Although I strongly recommend you use the answer of Siva Charan, just for information here's an implementation in LinQ using Enumerable.Aggregate Method (IEnumerable, Func):
var result = x.Aggregate((c, n) => c + "," + n);
Given an array like {"one two", "three four five"}, how'd you calculate the total number of words contained in it using LINQ?
You can do it with SelectMany:
var stringArray = new[] {"one two", "three four five"};
var numWords = stringArray.SelectMany(segment => segment.Split(' ')).Count();
SelectMany flattens the resulting sequences into one sequence, and then it projects a whitespace split for each item of the string array...
I think Sum is more readable:
var list = new string[] { "1", "2", "3 4 5" };
var count = list.Sum(words => words.Split().Length);
Or if you want to use the C# language extensions:
var words = (from line in new[] { "one two", "three four five" }
from word in line.Split(' ', StringSplitOptions.RemoveEmptyEntries)
select word).Count();
Not an answer to the question (that was to use LINQ to get the combined word count in the array), but to add related information, you can use strings.split and strings.join to do the same:
C#:
string[] StringArray = { "one two", "three four five" };
int NumWords = Strings.Split(Strings.Join(StringArray)).Length;
Vb.Net:
Dim StringArray() As String = {"one two", "three four five"}
Dim NumWords As Integer = Split(Join(StringArray)).Length