-1

Work on C# linq.I have a list to list.From this list I want to get the common items

List<SQLFrameWorkTableEntity> oParent = new List<SQLFrameWorkTableEntity>();

            List<List<SQLFrameWorkTableEntity>> oChild = new List<List<SQLFrameWorkTableEntity>>();
            oListParentTable = oList.Where(p => p.Parent == true).ToList();
            foreach (SQLFrameWorkEntity item in oListParentTable)
            {           

                oChild.Add(GetTableSchemaList(item, oParent));
            }

enter image description here Above picture describe how my oChild is. Now this oChild is the list of list SQLFrameWorkTableEntity.I want to get the common items from the oChild.How to get it?If have any query plz ask.Thanks in advance

After replace List to List> ,I write the bellow linq syntax

 var r=oChild.Select(p => p.Select(x => p.Any(y => y.ColumnName == x.ColumnName))).ToList();

Here, in r i get all list item are true,that means i fail to compare with list to list.Help me to correction this syntax.Thanks

shamim
  • 6,186
  • 19
  • 71
  • 139
  • What do you mean by "the common items from the `oChild`"? And why is `oChild` not strongly typed as List>` or something similar? It's not really clear what you mean. – Jon Skeet Nov 05 '11 at 20:04
  • thanks jon skeet ,i don't know this process List>.Thanks ,I change my syntax .Now will you plz tell me how to get the common items from the list of list.thanks again – shamim Nov 05 '11 at 20:10

1 Answers1

0

So you want to find the items that occur in all lists produced by GetTableSchemaList(item, oParent)? Let's assume yes.

First add Distinct() (if one list is not distinct yet):

oChild.Add(GetTableSchemaList(item, oParent).Distinct());

Then after the foreach do:

var count = oListParentTable.Count;
var r = oChild.SelectMany(i => i).GroupBy(i => i)
    .Where(g => g.Count() == count).SelectMany(i => i).Distinct();

The idea is that "common items" should occur exactly, say, 4 times in the flattened list of lists (SelectMany) when the number of distinct lists in oChild is 4.

Gert Arnold
  • 93,904
  • 24
  • 179
  • 256
  • excellent job ,thanks GertArnold.Hai!it's confuse me.I try to understand how it's work.Excellent .Thanks again.Will you plz show me some guideline to understand this kind of complex query. – shamim Nov 06 '11 at 16:54
  • Thanks! A guideline: training! What I did when LINQ was introduced is take the documentation on [IEnmerable](http://msdn.microsoft.com/en-us/library/9eekhta0.aspx) and do some exercises with each and every extension method. – Gert Arnold Nov 06 '11 at 17:59
  • After use your syntax i get common it's but common items are duplicate .i need unique list.will you plz extension your syntax. – shamim Nov 07 '11 at 13:25
  • You probably should do something like `GroupBy(i => i.ColumnName)`. So you group by ColumnName, which is a property that identifies a `SQLFrameWorkTableEntity`. – Gert Arnold Nov 07 '11 at 13:42
  • GertArnold thanks for reply.i already write syntax as oCommonList = oChild.SelectMany(i => i).GroupBy(i => i.ColumnName) .Where(g => g.Count() > 1).SelectMany(i => i).Distinct().ToList(); – shamim Nov 07 '11 at 13:49
  • This syntax gives me common duplicate item.Suppose list one and list two have common item=A.above syntax show me common A ,A on my list,but i want just one item show in list Just A – shamim Nov 07 '11 at 13:51
  • The problem is that your `A` objects are not the same instance, so Distinct() does not work. Use [this overlaod of Distinct](http://msdn.microsoft.com/en-us/library/bb338049.aspx). Make an IEqualityComparer that says that objects with the same ColumnName are equal. – Gert Arnold Nov 07 '11 at 14:17