0

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}
legend_blue
  • 93
  • 10
  • 3
    Possible duplicate of [how to merge 2 List with removing duplicate values in C#](http://stackoverflow.com/questions/4031262/how-to-merge-2-listt-with-removing-duplicate-values-in-c-sharp) – Matt Rowland May 16 '16 at 22:43
  • I dont think so. there is list object in this object another list – legend_blue May 16 '16 at 22:50
  • Then you need to explain the question better. From what you have explained it is a duplicate. – Matt Rowland May 16 '16 at 22:53

1 Answers1

3

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

Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
  • sorry about clarity of the question. ı need explain. This list contain Details and detailsvalue bunch of product. for example ı have clothes and perfume list ı get this items detail and detail values put together like : {Size{S,M,L},Clour{Green,blue},PerfumeSize{50ml,100ml},Type{EDP,EDT}} list like that ı have another list like this i need to merge together – legend_blue May 17 '16 at 07:07
  • @legend_blue Don't explain the question in a comment on an answer. Edit your question with this information. Also, don't just explain, show. – Matt Rowland May 17 '16 at 12:34
  • @legend_blue yes that's important, please edit the input and output requirements, so that correct linq can be devised, be little more elaborate – Mrinal Kamboj May 17 '16 at 13:31
  • @legend_blue, check my edit to your question and reply – Mrinal Kamboj May 17 '16 at 17:03