1

I have the following list into C#:

table_schema | table_name | field_name | field_type
public         tableA       fieldA       character varying
public         tableA       fieldB       timestamp
public         tableA       fieldC       bytea
public         tableB       fieldA       character varying
public         tableB       fieldD       integer
other          tableC       fieldA       character varying
other          tableC       fieldE       integer

So the field with name fieldA and type character varying exists to all.

The output should be a list object :

field_name | field_type
fieldA       character varying
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Stavros Koureas
  • 1,126
  • 12
  • 34
  • LINQ to objects or LINQ to SQL? Is the *real* question how to find the field+type that's common in all possible `schema.table` values? SQL's GROUP BY eliminates rows and can't be used to batch rows like this – Panagiotis Kanavos Feb 03 '23 at 09:36
  • The origin is coming from SQL some steps behind yes, but this result set exists into a list now. So I am focusing into pure lists. In addition I need to be sure that this field exists into all tables – Stavros Koureas Feb 03 '23 at 09:47
  • You still have to explain what you really want to do. `Found in all tables` isn't the same as `found in most tables` or `the most frequent common column`. It's probably easier and faster to *not* use LINQ but pass through the list a single time to identify the common column(s). LINQ would have to scan the list multiple times – Panagiotis Kanavos Feb 03 '23 at 10:06

1 Answers1

2

I suggest GroupBy and then Aggregate all these groups while Intersecting their content:

var list = ...

var result = list
  .GroupBy(item => (item.table_schema, item.table_name),
           item => (item.field_name, item.field_type))
  .Select(group => group.AsEnumerable()) 
  .Aggregate((current, item) => current.Intersect(item))
  .ToList(); // <- Let's have a list of tuples

The only trick is line

   .Select(group => group.AsEnumerable()) 

We don't want to aggregate by IGrouping<K, V> but by more general IEnumerable<V>

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Thanks for the answer but I do not think is working. I am getting more fields which are not common between all tables. – Stavros Koureas Feb 03 '23 at 10:11
  • @Stavros Koureas: If you suspect that the code doesn't work, could you provide a *counter example* please? I `Intersect` `(field_name, field_type)` across *all* the schemas so, the result will be `(field_name, field_type)` which present in *all* schemas – Dmitry Bychenko Feb 03 '23 at 10:13
  • @DimitryBychenko, the (field_name, field_type) should exists to all combinations of schemas and table_names not only schemas. – Stavros Koureas Feb 03 '23 at 10:17
  • @Stavros Koureas: I see; in this case, let's group by pair : `(item => (item.table_schema, item.table_name)`; I've edited the answer – Dmitry Bychenko Feb 03 '23 at 10:21
  • Thanks, I tried the same after your first post, its working now – Stavros Koureas Feb 03 '23 at 10:26