How do you return a group new {} linq query
-
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.
-
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.
You cannot pass anonymous types from one method to another. You'll have to create a normal class to hold the Description/FieldValue pair.
-
You cannot pass anonymous types from one method to another. You'll have to create a normal class to hold the Description/FieldValue pair.
-
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)[^]
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?)
-
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?)
-
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.
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.
-
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.
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.CreateADictionaryFromAnonymousTypeCreateADictionaryFromAnonymousType:
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: retMakeDictionary:
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 -
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.
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