Learning Learning - 3 years ago 134
C# Question

How to combine 2 list in to 1 list and get unique names from both the child of this 2 list?

I have a collection with 3 records in it:

Add=A1,A2,A3
Division=D1,D2,D3
Multiply=M1,M2,M3,A2

type=Add,Div,Multiply
names=A1,A2 etc...


Now I want to get all unique name of
Add
and
Multiply
in to single variable
and only list of names of Division in separate variable:

Expected output:

CombineList=A1,A2,A3,M1,M2,M3
DivisionList=D1,D2,D3


This is my query:

var TypesList = Ds1.Tables["dtSubVariant"].Rows.Cast<DataRow>()
.GroupBy(r => r["Type"] + "")
.Where(g => activeTypes.Contains(g.Key))
.Select(g => new {
type = g.Key,
names = g.ToList()
}).ToList();

var result = new
{
CombineList = (from t in TypesList
where t.type != "Division"
let name=t.names.Distinct()
select new
{
names=name
}).ToList(),
DivisionList = TypesList.Where(t => t.method == "Division")
.Select(t=>t.names).ToList()
};


But still I am getting 2 list in
Combine list
as below:

[0]:Add
A1,A2,A3
[1]:
Multiple
M1,M2,M3,A2


Can anybody help me with this please??

Answer Source

After better understanding what Learning wants the following is the answer. You want to group records in rows (in this example - in your code it is the Ds1.Tables["dtSubVariant"].Rows.Cast<DataRow>()) By something like the grouping list I've created. And then you want to have the distinct values of all the inner collections of each group:

List<dynamic> rows = new List<dynamic>
{
    new { Type = "Add", Values = new []{"A1","A2", "A3"} },
    new { Type = "Division", Values = new []{"D1","D2", "D3"} },
    new { Type = "Multiply", Values = new []{"M1","M2","M3","A1"} }
};

List<dynamic> grouping = new List<dynamic>
{
    new { Type = "CombinedList", Types = new [] {"Add", "Multiply"} },
    new { Type = "Division", Types = new [] {"Division"} }
};

var result = (from r in rows
              from g in grouping
              where ((IEnumerable<string>)(g.Types)).Contains((string)r.Type)
              group r by g.Type into rg
              select new
              {
                  Type = rg.Key,
                  Values = rg.SelectMany(item => (string[])item.Values).Distinct().ToList()
              }).ToList();

If you want a distinct list of Add and Multiply then use Union

List<string> Add = new List<string> { "A1", "A2", "A3" };
List<string> Multiply = new List<string> { "M1", "M2", "M3", "A2" };

var result = Add.Union(Multiply).ToList();

One can use Concat but that just adds puts the two lists together without removing duplicates


In the context of your code above, your problem is that each item in your TypesList has an inner collection - and you want that the merge of all those inner collection to be distinct. Use SelectMany and Distinct:

var combineList = TypesList.Where(t => t.type == "Add" || t.type == "Multiply")
                           .SelectMany(t => t.names)
                           .Distinct();

all together:

var result = new
{
     CombineList = TypesList.Where(t => t.type == "Add" || t.type == "Multiply")
                            .SelectMany(t => t.names)
                            .Distinct(),
     DivisionList = TypesList.Where(t => t.method == "Division")
                             .Select(t=>t.names)
};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download