Search code examples
c#linqazure-table-storage

Using LINQ query two tables bases on matching Ids and return a data structure that has the the id


Imagine a table structure like the above in Azure Table storage.

Table 1

Column A Column B Column C
"partitionkey1" 101 test
"partitionkey1" 102 test
"partitionkey1" 103 test
"partitionkey2" 201 test

Table 2 which is basically a special sublist of prime entities we want to add and remove from

Column A Column B Column C
"partitionkey1" 101 true
"partitionkey1" 102 true
"partitionkey1" 103 true
"partitionkey2" 201 false

Im looking to create a linq query along the lines of

if column A in t1 matches column a in table 2

return a collection of results in the format of ( the left list size is also 10 x the size of the right list )

AreaName rowkeys prime status
"partitionkey1" 101,102,103 true
"partitionkey2" 201 false
            var ACollection = await _ACollectionRepository.GetAll();
            var BCollection = await _BCollectionRepository.GetAll();

            var lft = ACollection.Where(x => !string.IsNullOrEmpty(x.PartitionKey)).ToList();
            var right = BCollection.Where( x=> !string.IsNullOrEmpty(x.PartitionKey)).ToList();

            var joinQuery = (from l in lft
                             join r in right on l.PartitionKey equals r.PartitionKey into joinedList
                             from item in joinedList.DefaultIfEmpty()
                             select new 
                             {
                                 l.PartitionKey,
                                 ///not sure what to do here ,
                                 item?.prime_status ?? false
                             });

Solution

  • GroupBy to group the items by partition key and then string.Join on to join the rowkeys

    RowKeys = string.Join(",", bGroup.Select(x => x.ColumnB)),
                    PrimeStatus = bGroup.Any(x => x.ColumnC)