LINQ query collects the values in keyValuePair object but then I can not save them all in the list

0

The following query LINQ takes all the parameters and all the stations and uses them to create a string and also stores them in a list of key pairs / value idestacion / parameter. To avoid confusion I have to clarify that this row:

  from x in selection.parameters

represents x stations, and each has x parameters

the first select takes well the X stations (selection.parameters) and their X parameters (x.parameters) in 'Result'

but in the second select nested in 'Values' only takes the parameters of the first station:

    var selectionData = from x in selection.Parameters
                        select new
                        {
                            Result = "(p.col_IdEstacion = " + x.Station + " 
                            and p.col_Sigla in(" + ("'" + string.Join("','", x.Parameters) + "'") +  "))",
                            Values = (from y in x.Parameters
                            select new KeyValuePair<string, string> 
                            (x.Station.ToString(), y)).ToList()
                        };

the reason why I think Values is to also take all the pairs of stations, parameter and put them in a list with addRange, I wanted to take all the pairs with LINQ

  List<KeyValuePair<string, string>>  myList

UPDATED : the LINQ is taking the values, but I am inserting them incorrectly in the list:

  valores.AddRange(selectionData.Select(d => d.Values).FirstOrDefault());

must be the FirstOrDefault () function that only takes one station, how could you add them all?

ANSWER : values.AddRange (selectionData. SelectMany (d = > d.Values)), Thanks Alberto was just what I needed

    
asked by Rarm 12.06.2018 в 14:23
source

1 answer

1

Your code works correctly.

public class Estacion
{
    public string Station { get; set; }
    public List<string> Parameters { get; set; }
}

public static class Selection
{
    public static List<Estacion> Parameters { get; set; } = new List<Estacion>();
}

I have assumed that these can be your classes (Ideally, you would publish them)

        Selection.Parameters.Add(new Estacion()
        {
            Station = "s1",
            Parameters = new List<string>()
            {
                "p1","p2","p3"
            }
        });

        Selection.Parameters.Add(new Estacion()
        {
            Station = "s2",
            Parameters = new List<string>()
            {
                "a1","a2","a3"
            }
        });

        var selectionData = from x in Selection.Parameters
                            select new
                            {
                                Result = "(p.col_IdEstacion = " + x.Station + " and p.col_Sigla in(" + ("'" + string.Join("','", x.Parameters) + "'") + "))",
                                Values = (from y in x.Parameters
                                          select new KeyValuePair<string, string>
                                          (x.Station.ToString(), y)).ToList()
                            };

And the object that I get is:

[
{
    "Result": "(p.col_IdEstacion = s1 and p.col_Sigla in('p1','p2','p3'))",
    "Values": [
        {
            "Key": "s1",
            "Value": "p1"
        },
        {
            "Key": "s1",
            "Value": "p2"
        },
        {
            "Key": "s1",
            "Value": "p3"
        }
    ]
},
{
    "Result": "(p.col_IdEstacion = s2 and p.col_Sigla in('a1','a2','a3'))",
    "Values": [
        {
            "Key": "s2",
            "Value": "a1"
        },
        {
            "Key": "s2",
            "Value": "a2"
        },
        {
            "Key": "s2",
            "Value": "a3"
        }
    ]
}
]

Tell me what you expect to get and I'll give you a hand.

    
answered by 13.06.2018 / 19:58
source