473,799 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ 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
OrderSubLineIte m

The result set I need is:
OrderHeader.Des cription, Count(OrderSubL ineItem) as CountAllItems

What I wanted to do is
var Result = from Item in DB.OrderHeader
where Item.OrderHeadI D = ID
select new {Item.Descripti on,
Item.OrderLineI tem.OrderSubLin eItem.Count}

I can't drill down from OrderLineItem to OrderSubLineIte m 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 3157
Chris wrote:
Trying to get my head around linq. Here is what I have:

Tables:
OrderHeader
OrderLineItem
OrderSubLineIte m

The result set I need is:
OrderHeader.Des cription, Count(OrderSubL ineItem) as CountAllItems

What I wanted to do is
var Result = from Item in DB.OrderHeader
where Item.OrderHeadI D = ID
select new {Item.Descripti on,
Item.OrderLineI tem.OrderSubLin eItem.Count}

I can't drill down from OrderLineItem to OrderSubLineIte m 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 OrderSubLineIte m.

You could try:
var q = from osli in DB.OrderSubLine Item
group osli by osli.OrderLineI tem.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.usenet NOS...@xs4all.n lwrote:
Chris wrote:
Trying to get my head around linq. *Here is what I have:
Tables:
OrderHeader
OrderLineItem
OrderSubLineIte m
The result set I need is:
OrderHeader.Des cription, Count(OrderSubL ineItem) as CountAllItems
What I wanted to do is
var Result = from Item in DB.OrderHeader
* * * * * * * * * where Item.OrderHeadI D = ID
* * * * * * * * * select new {Item.Descripti on,
Item.OrderLineI tem.OrderSubLin eItem.Count}
I can't drill down from OrderLineItem to OrderSubLineIte m 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 OrderSubLineIte m.

* * * * You could try:
var q = from osli in DB.OrderSubLine Item
* * * * group osli by osli.OrderLineI tem.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
21300
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
57289
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
1286
by: Neil | last post by:
Hi group, is there a special C# LINQ group available? Regards
0
1018
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... 2594ms vs 4579ms (total-CPU-time , based on 3773567 rows with a non-clustered index on the time column, having pre-fetched the data , on a multi-core server).
9
2505
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 about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
5
5512
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 in db.ActivityDatas where a.BeginDateTime >= From && a.BeginDateTime <= To where a.SubActivityId == sat group a by a.BeginDateTime.Month into mg select new { a.BeginDateTime.Month, count = mg.Count() };
0
1296
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
1193
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 of answering that question the responders always seem to launch into magnificent speaches about the speed and security of stored procedures and parameterised queries and their mother's apple pie. I'm not bitter, though. If you can tell me...
2
2400
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
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10250
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4139
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 we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.