Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. How do you return a group new {} linq query

How do you return a group new {} linq query

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqtutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sevententh
    wrote on last edited by
    #1

    Hi, I'm unsure how to return my linq query when using a grouping I have two queries: This gives me a flat list of rows

        public List<vw\_AFValueGridListing> Custom\_AFValueGridListBySPIDID(Int32 intSPIDItemID)
        {
            var q = from c in context.vw\_AFValueGridListing
                    where c.SPIDID == intSPIDItemID
                    orderby c.GroupName, c.DisplayOrder
                    select c;
    
            return q.ToList();
        }
    

    Now I would like to have the list split into groups but I'm not sure how to return this??

        public List<Object> Custom\_GroupedAFVList(Int32 intSPIDItemID)
        {
            var q = from c in context.vw\_AFValueGridListing
                    where c.SPIDID == intSPIDItemID
                    orderby c.DisplayOrder
                    group new
                    {
                        c.Description,
                        c.FieldValue
                    }
                    by c.GroupName;
    
            return q.ToList();
    
        }
    

    I have tested the query in LinqPad and it gives me the results I'm looking for.

    D L N 3 Replies Last reply
    0
    • S Sevententh

      Hi, I'm unsure how to return my linq query when using a grouping I have two queries: This gives me a flat list of rows

          public List<vw\_AFValueGridListing> Custom\_AFValueGridListBySPIDID(Int32 intSPIDItemID)
          {
              var q = from c in context.vw\_AFValueGridListing
                      where c.SPIDID == intSPIDItemID
                      orderby c.GroupName, c.DisplayOrder
                      select c;
      
              return q.ToList();
          }
      

      Now I would like to have the list split into groups but I'm not sure how to return this??

          public List<Object> Custom\_GroupedAFVList(Int32 intSPIDItemID)
          {
              var q = from c in context.vw\_AFValueGridListing
                      where c.SPIDID == intSPIDItemID
                      orderby c.DisplayOrder
                      group new
                      {
                          c.Description,
                          c.FieldValue
                      }
                      by c.GroupName;
      
              return q.ToList();
      
          }
      

      I have tested the query in LinqPad and it gives me the results I'm looking for.

      D Offline
      D Offline
      Daniel Grunwald
      wrote on last edited by
      #2

      You cannot pass anonymous types from one method to another. You'll have to create a normal class to hold the Description/FieldValue pair.

      L 1 Reply Last reply
      0
      • D Daniel Grunwald

        You cannot pass anonymous types from one method to another. You'll have to create a normal class to hold the Description/FieldValue pair.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You don't need to create a new class of object. You only need to cast the return type to an Object type before passing it. For more information see Microsoft's article on: Anonymous Types (C# Programming Guide)[^]

        D 1 Reply Last reply
        0
        • L Lost User

          You don't need to create a new class of object. You only need to cast the return type to an Object type before passing it. For more information see Microsoft's article on: Anonymous Types (C# Programming Guide)[^]

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          Of course you can cast it to object. But how do you cast it back so that you can access the members? (Yes, I know you can implement a method like "CastByExample" - but that's a horrible hack, why not just define a class?)

          L 1 Reply Last reply
          0
          • D Daniel Grunwald

            Of course you can cast it to object. But how do you cast it back so that you can access the members? (Yes, I know you can implement a method like "CastByExample" - but that's a horrible hack, why not just define a class?)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Hmm, well I did run into this problem. My solution was to use an extension method which does the same thing as the CastByExample. The implimentation was a bit cleaner.

            1 Reply Last reply
            0
            • S Sevententh

              Hi, I'm unsure how to return my linq query when using a grouping I have two queries: This gives me a flat list of rows

                  public List<vw\_AFValueGridListing> Custom\_AFValueGridListBySPIDID(Int32 intSPIDItemID)
                  {
                      var q = from c in context.vw\_AFValueGridListing
                              where c.SPIDID == intSPIDItemID
                              orderby c.GroupName, c.DisplayOrder
                              select c;
              
                      return q.ToList();
                  }
              

              Now I would like to have the list split into groups but I'm not sure how to return this??

                  public List<Object> Custom\_GroupedAFVList(Int32 intSPIDItemID)
                  {
                      var q = from c in context.vw\_AFValueGridListing
                              where c.SPIDID == intSPIDItemID
                              orderby c.DisplayOrder
                              group new
                              {
                                  c.Description,
                                  c.FieldValue
                              }
                              by c.GroupName;
              
                      return q.ToList();
              
                  }
              

              I have tested the query in LinqPad and it gives me the results I'm looking for.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              If LINQPad returned the correct result. You can click the 'IL' button to get a listing of the IL it generated to productt the result. Look at the method invocations it uses and try to duplicate the results. I have solved this problem in the past. I think I did some really strange things in an extension method where I emited a new assembly and class directly in IL. I got the solution for generating dynamic IL from CodeProject. Or you can also reflect on the Type to discover the underlying data passed. However, this is slower then emiting IL.

              L 1 Reply Last reply
              0
              • L Lost User

                If LINQPad returned the correct result. You can click the 'IL' button to get a listing of the IL it generated to productt the result. Look at the method invocations it uses and try to duplicate the results. I have solved this problem in the past. I think I did some really strange things in an extension method where I emited a new assembly and class directly in IL. I got the solution for generating dynamic IL from CodeProject. Or you can also reflect on the Type to discover the underlying data passed. However, this is slower then emiting IL.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Reflections Code:

                void Main()
                {

                CreateADictionaryFromAnonymousType();
                

                }

                void CreateADictionaryFromAnonymousType() {
                var dictionary = MakeDictionary(new {Name="ScottGu",Country="USA"});
                dictionary["Name"].Dump();
                }

                private IDictionary MakeDictionary(object withProperties)
                {
                IDictionary dic = new Dictionary<string, object>();
                var properties = System.ComponentModel.TypeDescriptor.GetProperties(withProperties);

                foreach (System.ComponentModel.PropertyDescriptor property in properties)
                {
                dic.Add(property.Name,property.GetValue(withProperties));
                }
                return dic;
                }

                IL Code:

                IL_0001: ldarg.0
                IL_0002: call UserQuery.CreateADictionaryFromAnonymousType

                CreateADictionaryFromAnonymousType:
                IL_0000: nop
                IL_0001: ldarg.0
                IL_0002: ldstr "Roy"
                IL_0007: ldstr "Israel"
                IL_000C: newobj <>f__AnonymousType0<System.String,System.String>..ctor
                IL_0011: call UserQuery.MakeDictionary
                IL_0016: stloc.0
                IL_0017: ldloc.0
                IL_0018: ldstr "Name"
                IL_001D: callvirt System.Collections.IDictionary.get_Item
                IL_0022: call LINQPad.Extensions.Dump
                IL_0027: pop
                IL_0028: ret

                MakeDictionary:
                IL_0000: nop
                IL_0001: newobj System.Collections.Generic.Dictionary<System.String,System.Object>..ctor
                IL_0006: stloc.0
                IL_0007: ldarg.1
                IL_0008: call System.ComponentModel.TypeDescriptor.GetProperties
                IL_000D: stloc.1
                IL_000E: nop
                IL_000F: ldloc.1
                IL_0010: callvirt
                System.ComponentModel.PropertyDescriptorCollection.GetEnumerator
                IL_0015: stloc.s 04
                IL_0017: br.s IL_003C
                IL_0019: ldloc.s 04
                IL_001B: callvirt System.Collections.IEnumerator.get_Current
                IL_0020: castclass System.ComponentModel.PropertyDescriptor
                IL_0025: stloc.2
                IL_0026: nop
                IL_0027: ldloc.0
                IL_0028: ldloc.2
                IL_0029: callvirt System.ComponentModel.MemberDescriptor.get_Name
                IL_002E: ldloc.2
                IL_002F: ldarg.1
                IL_0030: callvirt System.ComponentModel.PropertyDescriptor.GetValue
                IL_0035: callvirt System.Collections.IDictionary.Add
                IL_003A: nop
                IL_003B: nop
                IL_003C: ldloc.s 04
                IL_003E: callvirt System.Collections.IEnumerator.MoveNext
                IL_0043: stloc.s 05
                IL_0045: ldloc.s 05
                IL_0047: brtrue.s IL_0019
                IL_0049: leave.s IL_0068
                IL_004B: ldloc.s 04
                IL_004D: isin

                1 Reply Last reply
                0
                • S Sevententh

                  Hi, I'm unsure how to return my linq query when using a grouping I have two queries: This gives me a flat list of rows

                      public List<vw\_AFValueGridListing> Custom\_AFValueGridListBySPIDID(Int32 intSPIDItemID)
                      {
                          var q = from c in context.vw\_AFValueGridListing
                                  where c.SPIDID == intSPIDItemID
                                  orderby c.GroupName, c.DisplayOrder
                                  select c;
                  
                          return q.ToList();
                      }
                  

                  Now I would like to have the list split into groups but I'm not sure how to return this??

                      public List<Object> Custom\_GroupedAFVList(Int32 intSPIDItemID)
                      {
                          var q = from c in context.vw\_AFValueGridListing
                                  where c.SPIDID == intSPIDItemID
                                  orderby c.DisplayOrder
                                  group new
                                  {
                                      c.Description,
                                      c.FieldValue
                                  }
                                  by c.GroupName;
                  
                          return q.ToList();
                  
                      }
                  

                  I have tested the query in LinqPad and it gives me the results I'm looking for.

                  N Offline
                  N Offline
                  Niladri_Biswas
                  wrote on last edited by
                  #8

                  The type of var is an anonymous type. You can not return values of anonymous type from functions in C# 3.0. You should be able to mouse-over q in visual studio and see it's 'real' type (which you can define as your return type), but given your code, the value of the group is an anonymous class, which you can't use if you want a strongly-typed return value. I think this should work though:

                  public class GroupInfo
                  {
                  public string Description { get; set; }
                  public object FieldValue { get; set; }
                  }

                  public List<IGrouping<string, GroupInfo>> Custom_GroupedAFVList(Int32 intSPIDItemID)
                  {
                  var q = from c in new context.vw_AFValueGridListing
                  where c.SPIDID == intSPIDItemID
                  orderby c.DisplayOrder
                  group new GroupInfo()
                  {
                  Description = c.Description,
                  FieldValue = c.FieldValue
                  }
                  by c.GroupName;
                  return q.ToList();
                  }

                  I would think a Dictionary<K,List<V>> would be easier to work with as a result in a scenario like this than an IEnumerable<IGrouping<K,V>> though, and it's not hard to change to that:

                  public Dictionary<string, List<GroupInfo>> Custom_GroupedAFVList(Int32 intSPIDItemID)
                  {
                  var q = ....
                  return q.ToDictionary(i => i.Key, i => i.ToList());
                  }

                  :)

                  Niladri Biswas

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups