46778

what is wrong with my GroupBy Query

Hello all what is wrong with my GroupBy query ?

I have following class:

public class AssembledPartsDTO { public int PID { get; set; } public McPosition Posiotion { get; set; } public string Partnumber { get; set; } public string ReelID { get; set; } public int BlockId { get; set; } public List<string> References { get; set; } }

I am trying to perform following query:

assembledPcb.AssembledParts.GroupBy(entry => new { entry.PID, entry.Posiotion.Station, entry.Posiotion.Slot, entry.Posiotion.Subslot, entry.Partnumber, entry.ReelID, entry.BlockId }). Select( (key , val )=> new AssembledPartsDTO { BlockId = key.Key.BlockId, PID = key.Key.PID, Partnumber = key.Key.Partnumber, ReelID = key.Key.ReelID, Posiotion = new McPosition(key.Key.Station, key.Key.Slot, key.Key.Subslot), References = val <-- ???? })

But the val that I have there is of type int and not the val of grouping that I can do there val.SelectMany(v => v).ToList(); any idea what is wrong in my code ?

Answer1:

The second parameter of Enumerable.Select is the index of the item in the sequence. So in this case it is the (zero based) number of the group. You just want to select the group, you don't need it's index:

var result = assembledPcb.AssembledParts.GroupBy(entry => new { entry.PID, entry.Posiotion.Station, entry.Posiotion.Slot, entry.Posiotion.Subslot, entry.Partnumber, entry.ReelID, entry.BlockId }) .Select(g => new AssembledPartsDTO { BlockId = g.Key.BlockId, PID = g.Key.PID, Partnumber = g.Key.Partnumber, ReelID = g.Key.ReelID, Posiotion = new McPosition(g.Key.Station, g.Key.Slot, g.Key.Subslot), References = g.SelectMany(entry => entry.References) .Distinct() .ToList() });

(assuming that you want a list of distinct references)

Side-Note: you have a typo at the property-name: Posiotion

Recommend

  • OnTriggerStay not called every frame
  • Azure storage authorization failed or format is wrong
  • Breaking a video into frames with python
  • Pointer vs Reference difference when passing Eigen objects as arguments
  • LNK1104: cannot open file 'kernel32.lib'
  • CFBundleDevelopmentRegion not works as expected
  • Specifying virtual keyboard type for EditText in XML
  • Strong vs Weak entities MYSQL
  • Extending the Django 1.11 User Model
  • How to enable mapping the private property of the entity
  • how to change api_token column in token guard
  • Can my PDF ping my server when it is opened?
  • sweetalert2 inputoptions from file in select example
  • jQuery: How to AJAXify WordPress Search?
  • jwtBearer bearer token with rc-1 update to ASP.Net 5
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • MYSQ & MVC3 SQL connection error \\ ProviderManifestToken but I am using MySQL
  • Unable to get column index with table.getColumn method using custom table Model
  • PHP CURL timing out but CLI CURL works
  • Do I need to reset a Perl hash index?
  • Transactional Create with Validation in ServiceStack Redis Client
  • Hardware Accelerated Image Scaling in windows using C++
  • Why querying a date BC is changed to AD in Java?
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • Suggestions to manage Login/Logout transitions
  • How do you join a server to an Active Directory (domain)?
  • Why joiner is not used after Sequence generator or Update statergy
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Add sale price programmatically to product variations
  • Recursive/Hierarchical Query Using Postgres
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?