legend_blue
legend_blue

Reputation: 93

How to Merge two sub list with linq

I have 2 nested list Need to merge together ı try everthing using linq query ı try join and union ı didnt complete the query..Is there a way merge these the way i need?

public class BaseFilter
{
    public string Name { get; set; }
    public int ID { get; set; }
}
public class Filter
{
    public string Name { get; set; }
    public int DetailId { get; set; }
    public List<BaseFilter> Values { get; set; }
}

I have 2 Filter list like :

List 1:                        List2:

Clour :                        Colour:
     Red                             Red
     Blue
Size:                          Size:
      XL                             L
      M
      S

I want to merge list2 with one without duplicate record

Like : ist 1:

Clour :                      
     Red                           
     Blue
Size:                      
      XL                           
      M
      S
      L

Edit to understand the question better

My assumption is, you have table structure like (following two columns):

Details            DetailsValue

Size                 {S,M,L}
Colour             {Green,blue}
PerfumeSize     {50ml,100ml}
Type                {EDP,EDT}

Another similar table is there, now you want to merge them, without any value repetition, correct ?

Another list Details DetailsValue

Size                 {S}
Colour             {Red}
PerfumeSize     {50ml,150ml}
Type                {EDP}

expect result:

Details DetailsValue

Size                 {S,M,L}
Colour             {Green,blue,red}
PerfumeSize     {50ml,100ml,150ml}
Type                {EDP,EDT}

Upvotes: 0

Views: 1472

Answers (1)

Mrinal Kamboj
Mrinal Kamboj

Reputation: 11478

Question is quite confusing there's no clarity what you want to achieve, what is the relation between color and size, in list 1 they don't have one to one mapping data, nonetheless following are my suggestions to get a solution:

var result = list1.Concat(list2).GroupBy(x => x.Color,x => x.Size);

Here we are concatenating two lists, which would still lead to duplicated data, then you can Group By as shown above which will aggregate Size in this case based on Color. Size here would still be a collection in the result, which can be processed to give relevant result.

Other option, I can think of is Full Outer Join, where you need to define, which list data takes the precedence and what will be a default value, for data not available

Upvotes: 3

Related Questions