473,473 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Group By In LINQ

Trying to get my head around linq. Here is what I have:

Tables:
OrderHeader
OrderLineItem
OrderSubLineItem

The result set I need is:
OrderHeader.Description, Count(OrderSubLineItem) as CountAllItems

What I wanted to do is
var Result = from Item in DB.OrderHeader
where Item.OrderHeadID = ID
select new {Item.Description,
Item.OrderLineItem.OrderSubLineItem.Count}

I can't drill down from OrderLineItem to OrderSubLineItem though.

I am guaranteed that all LineItems have SubLineItems.

Also, I would like to return a dictonary of this and seem to have
having problems doing the ToDicontary when I do a select new at the
bottom of my LINQ.

Thanks
Chris

Oct 13 '08 #1
2 3145
Chris wrote:
Trying to get my head around linq. Here is what I have:

Tables:
OrderHeader
OrderLineItem
OrderSubLineItem

The result set I need is:
OrderHeader.Description, Count(OrderSubLineItem) as CountAllItems

What I wanted to do is
var Result = from Item in DB.OrderHeader
where Item.OrderHeadID = ID
select new {Item.Description,
Item.OrderLineItem.OrderSubLineItem.Count}

I can't drill down from OrderLineItem to OrderSubLineItem though.
If you need a given resultset, it's not about what you want to do, it's
about what you have to do. You can't 'drill down' to them as that
requires an m:1/1:1 relationship, and OrderLineItem has a 1:n
relationship with OrderSubLineItem.

You could try:
var q = from osli in DB.OrderSubLineItem
group osli by osli.OrderLineItem.OrderHeader.Description into g
select new { g.Key, CountAllItems = g.Count() };

Now you can drill 'up' as all relationships are m:1.

You can also create a subquery inside the projection, which is
effectively a scalar, though that leads to less efficient SQL.
I am guaranteed that all LineItems have SubLineItems.

Also, I would like to return a dictonary of this and seem to have
having problems doing the ToDicontary when I do a select new at the
bottom of my LINQ.
the select new creates an anonymous type. The ToDictionary() method
will return a dictionary with values of that anonymous type. This means
that you should do:

var dict = q.ToDictionary(v=>v.Key);

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Oct 14 '08 #2
On Oct 14, 3:02*am, "Frans Bouma [C# MVP]"
<perseus.usenetNOS...@xs4all.nlwrote:
Chris wrote:
Trying to get my head around linq. *Here is what I have:
Tables:
OrderHeader
OrderLineItem
OrderSubLineItem
The result set I need is:
OrderHeader.Description, Count(OrderSubLineItem) as CountAllItems
What I wanted to do is
var Result = from Item in DB.OrderHeader
* * * * * * * * * where Item.OrderHeadID = ID
* * * * * * * * * select new {Item.Description,
Item.OrderLineItem.OrderSubLineItem.Count}
I can't drill down from OrderLineItem to OrderSubLineItem though.

* * * * If you need a given resultset, it's not about what you want to do, it's
about what you have to do. You can't 'drill down' to them as that
requires an m:1/1:1 relationship, and OrderLineItem has a 1:n
relationship with OrderSubLineItem.

* * * * You could try:
var q = from osli in DB.OrderSubLineItem
* * * * group osli by osli.OrderLineItem.OrderHeader.Description into g
* * * * select new { g.Key, CountAllItems = g.Count() };

Now you can drill 'up' as all relationships are m:1.

You can also create a subquery inside the projection, which is
effectively a scalar, though that leads to less efficient SQL.
I am guaranteed that all LineItems have SubLineItems.
Also, I would like to return a dictonary of this and seem to have
having problems doing the ToDicontary when I do a select new at the
bottom of my LINQ.

* * * * the select new creates an anonymous type. The ToDictionary() method
will return a dictionary with values of that anonymous type. This means
that you should do:

var dict = q.ToDictionary(v=>v.Key);

* * * * * * * * FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website:http://www.llblgen.com
My .NET blog:http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Thanks for the information. Once I realized I needed to start at the
bottom and work up, the rest came together.

Chris
Oct 14 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: james | last post by:
Hi Guys, Is it possible to group by multiple Fields in LINQ? I want to do something similiar to SQL command: SELECT Count(*) FROM MyTable GROUP BY Field1, Field2 Thanks!
2
by: Ronald S. Cook | last post by:
I have a DataTable that looks like this: Id Value 123 4.0 123 5.0 234 1.0 345 2.0 345 3.0 I want to end up with (probably a new DataTable) that contains the sum of
5
by: Neil | last post by:
Hi group, is there a special C# LINQ group available? Regards
0
by: Marc Gravell | last post by:
I hadn't yet needed to do this, but I'm kinda glad it works! Shame about all those DATEADDs though... I've just run a test, and a CAST(FLOOR(CAST(x as float)) as datetime) is twice as quick......
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
5
by: Seb | last post by:
I want to count activity in a given month. I'm trying to do so with the linq code below however it reports: Error 1 'a' is inaccessible due to its protection level var ActivityByMonths = from a...
0
by: Nightcrawler | last post by:
I have the following tables that I have dragged into a .dbml file (have only included the keys for simplicity). CREATE TABLE .( NOT NULL) CREATE TABLE .( NOT NULL, NOT NULL)
3
by: Duncan A. McRae | last post by:
I am trying to find a simple answer to, "if I use LINQ against tables, must I give the webuser datareader/datawriter permissions in SQL Server?". I see the question asked repeatedly, but instead...
2
by: shapper | last post by:
Hello, I have the following Linq query: List<PostsTaginsert = (from t in (from t in database.Tags join p in paper.Tags on t.Name equals p.Name select t).ToList() select new PostsTag {
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.