Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq query for EAV

Tags:

c#

linq-to-sql

I have a big (4M records) table on a SQL Server with the following columns:

  • Id (a record identifier);
  • PropertyName (a string that represents a property name);
  • PropertyValue (an integer that represent the property's value);

keep in mind that Id is not unique for this table. Basically, I'd like to write a LINQ query that retrieves Ids and all its associated PropertyName/PropertyValue pairs:

  • Id
  • dictionary (maybe?) PropertyName -> PropertyValue;

is this feasible in some way?

Thank you in advance

like image 814
Simone Avatar asked Jul 02 '26 03:07

Simone


1 Answers

Not sure regarding performance but you can use GroupBy to group your data by similar Ids

Something like:

var result =
  from x in whatever
  group x by x.Id into g
  select new { 
         Id = g.Key, 
         Data = g.ToDictionary(i => i.PropertyName, i => i.PropertyValue)
  };

Refer to 101 LINQ Samples to learn more about Linq

like image 146
aku Avatar answered Jul 03 '26 16:07

aku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!