Linq InvalidCastException For Group By

Dec 12, 2010

I have a Linq Group By query that works. Here's the query:

Dim query = From fb As Feedback In lst Where fb.Seller.login_name.ToLower = UserName.ToLower
Order By fb.transaction_id Descending, fb.creation Descending _

[Code]...

I don't like working with anonymous types so I'm trying to delare the result and am hitting an InvalidCastException.

Here's the type information:

Debug.Print(query.GetType.ToString)
Returns:
System.Linq.GroupedEnumerable`4[Feedback,System.Nullable`1[System.Int32],Feedback,VB$AnonymousType_0`2[System.Nullable`1[System.Int32],System.Collections.Generic.IEnumerable`1[Feedback]]]

[Code].....

View 2 Replies


ADVERTISEMENT

Linq InvalidCastException For Group By?

Aug 12, 2011

I have a Linq Group By query that works. Here's the query:

Dim query = From fb As Feedback In lst Where fb.Seller.login_name.ToLower = UserName.ToLower
Order By fb.transaction_id Descending, fb.creation Descending _
Group fb By fb.transaction_id _
Into Group

I don't like working with anonymous types so I'm trying to delare the result and am hitting an InvalidCastException.

Here's the type information:

Debug.Print(query.GetType.ToString)

Returns:

System.Linq.GroupedEnumerable`4[Feedback,System.Nullable`1[System.Int32],Feedback,VB$AnonymousType_0`2[System.Nullable`1[System.Int32],System.Collections.Generic.IEnumerable`1[Feedback]]]

and the inner type:

Debug.Print(item.GetType.ToString)

Returns:

VB$AnonymousType_0`2[System.Nullable`1[System.Int32],System.Collections.Generic.IEnumerable`1[Feedback]]

So, armed with this information, here's the declaration used:

Dim query As IEnumerable(Of IGrouping(Of Int32?, IEnumerable(Of Feedback)))

Here's the error returned:

Unable to cast object of type 'System.Linq.GroupedEnumerable`4[Feedback,System.Nullable`1[System.Int32],Feedback,VB$AnonymousType_0`2[System.Nullable`1[System.Int32],System.Collections.Generic.IEnumerable`1[Feedback]]]' to type

[code]....

View 4 Replies

Asp.net - Linq To SQL: Group By And Sum()?

Aug 9, 2010

I have the following LINQ statement, which performs a simple linq query and assigns the resulting values labels on an asp.net web form:

Dim db As New MeetingManagerDataContext
Dim q = From s In db.vwRoomAvailabilities _
Where s.MeetingID = lblMeetingID.Text _

[code]....

Originally, there was going to be only a single row result and you can see I'm using FirstOrDefault() to grab that single value which works great. But the design has changed, and multiple rows can now be returned by the query. I need to now Group By the MeetingID above, and SUM each of the selected columns (i.e. s.AllRequestedDoubles).

View 1 Replies

LINQ Group By SUM

May 28, 2012

[code]how can i group by SUM of the c.ct?

View 1 Replies

.NET LINQ Group Joining?

Dec 2, 2010

I have a list of object with 3 properties (quantity, service, name)I want to have all names and services in my list (kinda cross join) and the grouped quantity of the row corresponding. Not clear i guess

Quantity Service Name
3 Srv2 Bob
4 Srv2 Paul
2 Srv1 Paul
1 Srv2 Nick

I want as output All the services and all the names and corresponding quantities (0 if none)

Srv1 Paul 2
Srv1 Bob 0
Srv1 Nick 0

[code]....

Here is what I got so far, but I dont even get the expected resultsAnd I am acutally certain there is a pretty easy way of achieving what i want...

Dim services = (From a In interventions Select New With {.Service = a.Service}).Distinct()
Dim months = (From b In interventions Select New With {.Month = b.DateHeureIntervention.Month}).Distinct()
'Dim query = (From s In services _

[code]....

View 1 Replies

C# - LINQ To SQL Group People By Age?

Nov 3, 2011

Sample in C# and VB.NET are OK.

I have a table "People" with the following columns:

-FullName (nvarchar not null)
-DOB (datetime null)

I want to write a LINQ to SQL to group the people by age, like following result:

Age 19: 4 ppl
Age 20: 5 ppl
Age 21: 6 ppl
Here's my try:
Dim query = From ppl In db.People _
Select New With {.Age = DateTime.Now.Year - CDate(ppl.DOB).Year, .CountAge = ppl.Count}

Notice that there are no DOB record for some people in the tables, so these shall not be included. The DOB column has record like this 1982-10-24 10:12:45 AM because it's a DateTime column.

View 4 Replies

C# - Using LINQ To SQL Group, Sum And Aggreate All Together

Aug 18, 2010

I have two tables Students and Origami. Origami has Foreign Key of Students table. Each student can make one or more origami for each month.

[code]...

This query give only total origami of each student. But I want monthly count of origami for each student.

View 2 Replies

Using Linq Group Joins In .Net?

Oct 18, 2011

I'm trying to figure out how to use Group Joins in LINQ queries under VB.net. For some reason, every example I seem to find on the syntax is just plain At least, that's what my compiler keeps telling me. This is a simple example where I want to join orders to their order items so that I end up with a type that contains a collection of order items grouped together by their orderId's:

Dim groupedOrders = (From o In orders
Group Join i In orderItems On o.OrderId Equals a.OrderId Into myOrders
Select o.OrderId, myOrders).ToList()

What I'm currently running into in this example is that the 'myOrders' group I'm creating errors out with: Definition of method 'myOrders' is not accessible in this context.

View 2 Replies

VS 2010 LINQ To SQL Group By?

Nov 16, 2011

In proper sql my query would be something like:

select c.id, c.name, count(i.id)
from cats c, items i
where c.id=i.catid

[code].....

View 1 Replies

Linq Doing A Group In A Lambda Subselect

Feb 23, 2012

I have a user input screen that allows them to select input values and the resulting query is dependent on which values the user entered. The resulting query groups up the results to present a list of unique customers

Simplified table design - A customer can be in many states
[code...]

View 1 Replies

C# - Pick Out A Group Of Items Using Linq To Xml

Jul 4, 2011

My head is fuzzled with this. I have an xml doc which has the layout for a grid stored in it. If you notice the columns are stored as "Items" in the XML. I am trying to retrieve each "Item" out of the XML using LINQ but no matter what I do I keep taking on straggler properties that I don't need.

[Code]...

View 1 Replies

Get A Linq To Sql Group By Query Into The Asp.net Mvc View?

Dec 23, 2009

I have the following query that groups parking spaces by their garage, but I can't figure out how to iterate the data in the view.

Public Function FindAllSpaces() Implements ISpaceRepository.FindAllSpaces
Dim query = _
From s In db.spaces _
Order By s.name Ascending _

[code]....

The controller is taking the query object as is and putting it into the viewdata.model and as stated the view is not currently strongly typed as I haven't been able to figure out how to do this. I have run the query successfully in linqpad.

View 2 Replies

Linq - Group By Multiple Columns?

Dec 6, 2011

I have a list of attachments that I need to group by clientCLID and EmailAddress. From this grouped list I only need a list of clientCLIDs. After fiddling for a while I've managed to get it to work as follows:

[Code]...

View 1 Replies

LINQ Group By With Multiple Properties?

Jun 10, 2011

I spent a lot of time on this problem. I am able to do simple Group By LINQ queries (on one property) but for multiple fields I'm a little stuck... Here is a LINQPad sample of what I want to do :

dim lFinal={new with {.Year=2010, .Month=6, .Value1=0, .Value2=0},
new with {.Year=2010, .Month=6, .Value1=2, .Value2=1},
new with {.Year=2010, .Month=7, .Value1=3, .Value2=4},

[Code]....

View 3 Replies

Linq Group Count Items?

Dec 5, 2011

I have this linq to entity

From r In ReceiptRepository.Fetch
Where
r.RECEIPTDATE >= ReportStartDate And

[Code]....

This is working fine, except the count property, it is just giving the number of group count. I don't know how to find out each Tender Count

View 1 Replies

LINQ To SQL Using GROUP BY And COUNT(DISTINCT)

Jan 15, 2009

I have to perform the following SQL query: select answer_nbr, count(distinct user_nbr)
from tpoll_answer where poll_nbr = 16 group by answer_nbr The LINQ to SQL query

[Code]...

View 5 Replies

Sql - LINQ Group By Aggregating The Rows In .net?

May 10, 2012

I have a table that has a 2 part key; an ID column, and a version number. I'm trying to select 'only the latest version' of the records.I'm trying to use the expression style of LINQ, to achieve a GROUP BY, and pull out the aggregate MAX value of the Version. However no aggregation is taking place.

[Code]...

View 2 Replies

VS 2008 - LINQ Group By Datatable

Feb 21, 2012

syntax of the LINQ query to use GROUP something BY something and select some fields of the datatable. i am having a datatable filled with some rows, i am trying to fill the datagridview with the GROUP BY query output of some fields.

View 4 Replies

Can't Use A Where Clause On Both Tables In A LINQ Group Join?

Jun 7, 2011

Here's what I want to do:

Dim queryX = From m In db.Master
Where m.Field = value
Group Join d In db.Detail
On m.Id Equals d.MasterId Into Group
Where d.Field = value

In English, I want to join the master and detail tables, specifying conditions on each. But this causes the following compiler error:

"Name 'd' is either not declared or not in the current scope."

It works if I put this same condition in functional form:

Group Join d In db.Detail.Where(Function(x) x.Field = value)

but I think this is more typing, harder to understand, and introduces that irritating dummy variable. I really would prefer to use the query comprehension syntax. Is there a way to accomplish this?

View 1 Replies

Group By Over Anonymous Type With Linq To Object?

Feb 25, 2010

I'm trying to write a linq to object query in vb.net, here is the c# version of what I'm trying to achieve (I'm running this in linqpad):

void Main()
{
var items = GetArray(

[code].....

View 1 Replies

How To Group And Merge List Of Data Using LINQ

Apr 19, 2011

I have a list of data as shown below

Original Data
Genre Name Year
Comedy Shrek Forever After 2010
Drama The Karate Kid 2010
Action Iron Man 2 2010

[Code]...

View 1 Replies

How To Group Distinct Columns In A Linq Query

Nov 4, 2010

The following data is created by joining two sql tables together:I would like to group together distinct rows of DateStamp/UserName/ StudentName/ InstructorName/TableName/PrimaryKey (I'll call this 'group records') and then group under these ColumnName/PreviousValue/NewValue (I'll call this 'subgroup records')The end result would be that I could iterate through the 'group records' - there would be 5. In each 'group record', I could then iterate through the 'subgroup records'. The 5 groups would contain 3, 2, 5, 2 and 1 subgroup records respectively.What would be the syntax to create a query to do this? Ideally this would be in a vb.net linq syntax.

View 1 Replies

Linq Group By And Order By Multiple Conditions?

Jan 16, 2012

I have the following:

myFilteredContractors = (From c In myFilteredContractors
Join cc In myConClasses On c.ContractorId Equals cc.ContractorId
Where inClassifications.Contains(cc.ClassificationId)[code].....

This is properly ordering this list of contractors by the number of classifications that they have.I also want to order them by whether or not they have a field set (CompanyOverview), which if is an empty string should be below those contractors who have set their CompanyOverview. Also, after the CompanyOverview is ordered I want to order by Registration Date.So it should order by:

Number of Classifications
Whether Overview has been set (c.CompanyOverview)
Registration Date (c.AppliedDate)

Is it possible to all of this in LINQ?

View 1 Replies

Linq Group On Multiple Fields - Anonymous - Key

Apr 3, 2012

I am stumped. I have a DTO object with duplicates patient address data. I need to get only the unique addresses.

[Code]...

View 3 Replies

Order By And Group By In A Linq To Objects Query?

Sep 28, 2010

How do I order by and group by in a Linq query?I tried..Dim iPerson = From lqPersons In objPersons Where Len(lqPersons.Person) > 0 Group lqPersons By key = lqPersons.Name Into Group Order By Group descending Select Group, key

For Each i In iPerson
tmp = tmp & vbNewLine & i.key & ", " & i.Group.Count
Next

[code].....

View 5 Replies

Using Linq To Group, Order And Concatenate Strings

Jun 14, 2011

Suppose I have the following data

Key ID Data
A 1 Hello
A 2 World
B 2 Bar
B 1 Foo

I am looking to produce the result

A HelloWorld
B FooBar

I am struggling to get the syntax quite right - I was trying to use Aggregate, but I wasn't sure if I could (or should) use SelectMany

Dim data = result.Rows.
GroupBy(Function(r) r.Key).
Select(Function(g) g.OrderBy(Function(s) s.ID)).

[Code]....

View 4 Replies

C# - Linq Group By With Anonymous Types Not Working As Expected?

Jul 14, 2011

I was toying around with some of the linq samples that come with LINQPad. In the "C# 3.0 in a Nutshell" folder, under Chater 9 - Grouping, there is a sample query called "Grouping by Multiple Keys". It contains the following query:

from n in new[] { "Tom", "Dick", "Harry", "Mary", "Jay" }.AsQueryable()
group n by new
{

[Code].....

View 1 Replies

Group Joins With Multiple Tables Converting SQL To Linq

Mar 7, 2011

Can anyone help me by translating this SQL Statement to Linq? [code]

View 1 Replies

LINQ - Validating All Textfields Of Two Group Boxes Have Information?

Mar 25, 2011

It appears to me to be right but always returns true.

vb
Private Function ValidateAllFieldsAreNotEmpty() As Boolean
Dim groupboxes = Me.Controls.OfType(Of GroupBox)()
Dim textboxes = groupboxes.SelectMany(Function(groupbox)

[code]....

View 5 Replies

LINQ Objects - Group All The MyObjects Together That Share The Same Value And Then Return That Value?

Nov 16, 2010

I am having trouble following some of the examples at MSDN for LINQ.Basically I have a List Of MyObject. MyObject has a property called Value which is an integer.I want to group all the MyObjects together that share the same Value and then return that Value where the count of the group is greater than 5.I have tried the following,

dim foak = From o as MyObject in MyObjectList _
Group o By o.Value into Count _
WHERE Count > 5 _
Select o.Value

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved