LINQ To SQL Grouping Multiple Columns With A Distinct Row

Aug 9, 2010

I have the following table structure. I want to select distinct CustomerId and CustomerName, TotalCost.[code]

View 2 Replies


ADVERTISEMENT

Linq To Datarow, Select Multiple Columns As Distinct?

Apr 16, 2010

basically i'm trying to reproduce the following mssql query as LINQ

SELECT DISTINCT [TABLENAME], [COLUMNNAME] FROM [DATATABLE]

the closest i've got is

Dim query = (From row As DataRow In ds.Tables("DATATABLE").Rows _
Select row("COLUMNNAME") ,row("TABLENAME").Distinct

when i do the above i get the error

Range variable name can be inferred only from a simple or qualified name with no arguments.

i was sort of expecting it to return a collection that i could then iterate through and perform actions for each entry. maybe a datarow collection?

As a complete LINQ newb, i'm not sure what i'm missing.
i've tried variations on

Select new with { row("COLUMNNAME") ,row("TABLENAME")}

and get:

Anonymous type member name can be inferred only from a simple or qualified name with no arguments.

to get around this i've tried

Dim query = From r In ds.Tables("DATATABLE").AsEnumerable _
Select New String(1) {r("TABLENAME"), r("COLUMNNAME")} Distinct

however it doesn't seem to be doing the distinct thing properly.

View 3 Replies

Asp.net - Select Distinct Rows From A Datatable With Criteria Involving Multiple Columns Using LINQ

Mar 9, 2012

I have a datatable as shown in the figure. Let me explain my required based on this image. I have 7 rows of data. The rows 1 and 2 contains columns till UnitSqcNo same. I want only row 3 among the two. In general I want select all the rows with distinct model, unittype, unit and rest with greater CompId. ie the table should look like

View 1 Replies

C# - Get Distinct Rows From Datatable Using Linq (distinct With Mulitiple Columns)

Jul 13, 2010

I am trying to distinct on multiple columns and get datarows from datatable. but getting error.

Dim query As IEnumerable(Of DataRow) =
(From row As DataRow In SourceTable.AsEnumerable() _
Select row.Field(Of String)("ColumnName1"),

[Code]....

I want another datatable with distinct row based on given columns from SourceTable.

View 3 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 Distinct Count Multiple Fields

Mar 24, 2012

I have a Table:

[Code]...

I'm trying by LINQ-query (VB.NET) to return a count for distinct value of Objects, Stuffs and Colours for every Room:

[Code]...

View 1 Replies

LINQ To SQL Select Distinct From Multiple Colums?

Apr 2, 2010

I'm using LINQ to SQL to select some columns from one table. I want to get rid of the duplicate result also.

Dim customer = (From cus In db.Customers Select cus.CustomerId, cus.CustomerName).Distinct

Result:

1 David
2 James
1 David
3 Smith
2 James
5 Joe

Wanted result:

1 David
2 James
3 Smith
5 Joe

Can anyone show me how to get the wanted result?

View 3 Replies

C# - LINQ Select Distinct While Evaluating Multiple Rows

Jul 11, 2011

I have an EnumerableRowCollection that looks like the following:
VendorCode | GroupType | Variance
01165 G .16
01165 G .16
01165 CH .16
01165 CH .18
07754 G .25
07754 G .25
07754 G .39

Essentially, this is a massive list of vendor codes, their groups, and price variances. I need to compose a query that will create a distinct list vendor codes and group types. The catch, however, is that I need to evaluate all of the variances associated with that particular VendorCode/GroupType to see if they are all the same - it they are not, I need to return some way of signifying that the group has a "custom" variance, otherwise it needs to return the value (ie: if they are all .16, then return .16, if there are multiple values, return "custom")

The result would look like this, based off of the list I showed above.
VendorCode | GroupType | Variance
01165 G .16
01165 CH custom
07754 G custom

I have no trouble getting a distinct list of VendorCode/GroupType - this is what I have so far:
Dim distinctList = From q In query Select q.VendorCode, q.GroupType, (evaluated q.Variance here?) Distinct
(where "query" is an EnumerableRowCollection(Of (anonymous type)))
I'm at a loss, though, on how to evaluate the variance property to get the result that I need?

View 1 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

C# - LINQ To SQL Join 3 Tables And Select Multiple Columns And Also Using Sum

Aug 19, 2010

I have three tables Student, TimeSheet and TimeRecord.

Talbe columns:
Student : StudentId, FirstName,
LastName
TimeSheet: TimeSheetId,StudentId, IsActive

[Code].....

View 4 Replies

Linq - Order By Multiple Columns Using Lambda Expressions

Nov 16, 2009

I'm still trying to get my head around the whole "Lambda Expressions" thing.

Can anyone here give me an example ordering by multiple columns using VB.Net and Linq-to-SQL using a lambda expression?

Here is my existing code, which returns an ordered list using a single-column to order the results:

Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).ToList

Note: The WebCategory object has a child WebPage object (based on a foreign key). I'd like to order by WebPage.DisplayOrder first, then by WebCategory.DisplayOrder.

I tried chaining the order bys, like below, and though it compiled and ran, it didn't seem to return the data in the order I wanted.

Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).OrderBy(Function(c As WebCategory) c.WebPage.DisplayOrder).ToList

View 2 Replies

C# - Get A Result In A Grouping Query Which Is Value Agnostic And Has Dynamic Columns?

May 17, 2011

I have a table called Foo and I have two columns: Lorem and Ipsum, so the scheme of the table is:

Foo(Lorem, IpsumID)

I also have a table called IpsumTypes, which looks like this:

IpsumTypes(IpsumID, IpsumName)

I would like to write a LINQ query which will have the following result:

Headers: Lorem, IpsumName1, IpsumName2, ..., IpsumNamen
Values: Loremi, count(IpsumName1), ..., count(IpsumNamen)

Each row represents a group of Lorem value and the number of each possible IpsumID in the group. New rows can be added to the IpsumTypes table, rows can be deleted too, so I need dynamically generated columns, because at the time of writing the code I'm not aware of the possible values of Ipsum. How can I achieve this? Is there a magical Linq query which solves this problem, or should I get the values from the database and parse them separately, if speed counts?

View 2 Replies

.net - Grouping Using LINQ?

Feb 5, 2010

I'm having a heck of a time with transforming a simple SQL Query into a LINQ query(using vb btw)

Here is my SQL:
SELECT USRDEFND5
FROM int_gp_employee
GROUP BY USRDEFND5

[Code]...

I've tried a number of different variations of the LINQ. My current statement is:

From b In xmlFile...<row> Group b...<usrdefnd5> By b...<usrdefnd5> INTO group when I foreach through the resulting collection, EVERY line (17000) shows up.

View 2 Replies

Asp.net - Generate Columns From Distinct Values?

Oct 25, 2011

I have a three column table which lists every person, every event they may have attended, and what percentage of the time they were there. I would really like to display this on one page with the person names on the side and the event names across the top.

Here' an example of what I have:

NAME EVENT %ATTENDANCE
Smith Rock Climbing 50
Allen Rock Climbing 78

[Code]....

View 2 Replies

DISTINCT Output From Multi-Columns?

Aug 4, 2010

I'm trying to fill a ComboBox from 3 columns in the same table. I need output DISTINCT and the drop down box would look better if it had no spaces between groups.

Using myCommand As New MySql.Data.MySqlClient.MySqlCommand("SELECT ten_id1, ten_id2, ten_id3 FROM tenants", myTenConnection)
Dim myReader As MySql.Data.MySqlClient.MySqlDataReader

[Code].....

View 2 Replies

.net - Linq Nested Grouping?

Jul 7, 2009

I've a large table of Items and I need to organize them by Category, then by Year and then by Month.Item has CategoryID and Dated properties.I got this far:

Dim Items = From Item In DB.Items _
Group By CategoryID = Item.CategoryID _
Into Categories = Group _
Order By CategoryID

But where I put the:

Group By Year = Year(Item.Dated)

and the

Group By Month = Month(Item.Dated)

The final result should be something like this:

For Each Category in Categories
For Each Year in Category.Years

[code].....

View 2 Replies

Datatable Grouping Using Linq?

May 25, 2011

I'm completely dense here, but I'm trying to get some stats from a DataTable. One of the columns in the datatable is called "colour".I need to find out how many of each instance of "colour" are in the datatable.

I'm trying:
Dim q = From p In PGWorkingDataTable _
Group p By p("colour") Into Group _

[code].....

View 2 Replies

Counting Orders And Grouping Them, Linq To Sql?

Aug 15, 2011

I have a table called Ordersthe table has many rows but im only interested in two in particular.I want to query the table so it returns the average ordered count of each producti want to know what is the average count of the ordered products, but now in total but per productID instead.

View 1 Replies

Grouping A Generic List Via LINQ?

Jun 26, 2011

I need to take a collection and group it via Linq but all the examples I've seen fail in some manner or other with some syntax difference that I can't quite lick.

My collection:

Dim a As New List(Of ProcessAlert)
a.Add(New ProcessAlert("0000112367", "5551110000@txt.att.net", "Alert", 2))
a.Add(New ProcessAlert("0000112367", "5551110000@txt.att.net", "Document", 2))

[Code].....

View 1 Replies

Linq Grouping By Nullable Datetime And Using This As Criteria?

Nov 17, 2009

I am struggling with a nullable datetime column [DateInsp] in an ASP.NET app which uses SubSonic3, Linq, MS SQL Server 2005.I had this all working when the datetime column [DateInsp] did not allow nulls. A new requirement forced me to set the [DateInsp] column to allow nulls and now I am struggling getting this piece of functionality to work properly again.

Problem 1:I need to first render a dropdown list of the 7 most recent inspection dates for a given inspector (this is a grouped list of the 7 most recent dates for the inspector). Here is the TSQL that I need to convert to Linq syntax:

declare @InspectorID varchar(5)
set @InspectorID = 'GPA'
select top 7 convert(nvarchar(30), [DateInsp], 101) InspectedDate

[code]....

If I can't get this work properly using Linq, BUT I can/could build a stored proc to return the list of dates and throw that into a dropdown. Fair enough. I've been fighting with the Linq syntax?

Problem 2: I need to use the selected date in the dropdown mentioned above to pull the correct records for this inspector and the correct date. Again, this is a nullable datetime field and this is real sticking point for me.

Here was the original Linq syntax that accomplished the requirement before I had to change the datetime field to allow Nulls:

Dim query = (From i In db.IncomingInspections _
Where i.InspectorID = User.Identity.Name _
Group By Key = New With {i.DateInsp} Into Group _

[code]....

For some reason I can't use the .Value property; I get: The member 'Value' is not supported I get: "Incorrect syntax near '<'." if I try to add the date comparison to the where clause too.

View 2 Replies

Return Max Record Count By Grouping In Linq?

Nov 9, 2011

I'm very new to linq and I'm trying to come up with a linq query against a dataset that will return a max count value based on grouping records.[code]...

View 1 Replies

Distinct When Using Union On LINQ?

Nov 18, 2010

When the user has typed in some information I would like to output possible matches in a descending order, so if someone types in a full first name and a full surname, it should be listed above a result where just the surname matches.I've done something similar in SQL before which worked perfectly, but this time I'd like to do it in LINQ.

Firstname, Surname, City, Country as string variables.
Dim DataEnum As IEnumerable(Of frmTelephone.clsPerson) = alPerson.OfType(Of frmTelephone.clsPerson)()

[code].....

View 4 Replies

Converting A Traditional SQL Aggregate Query Into A LINQ One - SUM Grouping ?

May 24, 2010

I'm having trouble getting my head around converting a traditional SQL aggregate query into a LINQ one. The basic data dump works like so:

Dim result =
(From i As Models.InvoiceDetail In Data.InvoiceDetails.GetAll
Join ih As Models.InvoiceHeader In Data.InvoiceHeaders.GetAll On i.InvoiceHeaderID Equals ih.ID

[CODE].....................

What I need to really be getting out is ih.Period (a value from 1 to 12) and a corresponding aggregate value for i.ExtendedValue. When I try to Group ih I get errors about i being out of scope/context.

View 1 Replies

Linq Grouping With Anonymous Types And Option Strict

Apr 24, 2012

I've got the below, where I'm grouping a collection by a propery, then wanting to access the properties of my group. Problem I have is with the anonymous typing. Because of option strict I have to give an explicit type but I can't work out what the type should be. The below doesn't compile because t.HeadAccountKey isn't a value (as t has a type of object). So either I need do some casting or my linq selector is wrong,

[Code]....

View 1 Replies

.net - Linq To Object - Select Distinct

Mar 18, 2010

I can't quite figure out why this Linq Statement isn't working as i would expect:

[Code]....

I would assume that this would create a new collection of anonymous types, that would be distinct. Instead it creates a collection the size of the "ThisParentCollection" with duplicate "MyAnonymousType" in it (duplicate id's).

View 1 Replies

.net - LINQ To XML And Distinct Custom Class?

Oct 14, 2009

I have a very interesting LINQ question. I have a document, that I am trying to filter results on, but to filter, I am matching on a REGEX result from one element of the XML. I have the following, working LINQ to XML to get the individual data that I'm looking for.

[Code]...

View 3 Replies

Get Distinct Values From List(Of T) Using Linq?

Mar 10, 2012

I have a List(Of Hardware) - the List is called HWModels

Class Hardware has the following Properties:

ModelName
Status
CPUStatus
MemoryStatus
DiskStatus

The List is populated by reading a CSV file, once it's populated, I want to return the distinct records based on the ModelName

I've attempted by doing it as follows:

(From a In HWModels Select a.ModelName).Distinct

But this isn't right because I end up with a list of only the ModelName's and nothing else.

How do I get the Distinct function to return all of the other class members within the list?

View 1 Replies

LINQ / Select Distinct From Dataset?

Aug 20, 2010

I have a single columned datatable inside a single tabled dataset.I just want to convert this dataset to distinct rows. Here is my code, it gives compile error '.' expected.

Dim query = _
From email In ds.Tables(0) _
Select email.Field<string>("Email").Distinct()

EDIT: I changed to (Of String) and it works... BUT NOW 'query' is an ienumerable collection of characters... not a datatable... so how do I convert back easily without manually doing a loop?

View 1 Replies

Linq Join On Parameterized Distinct Key?

Sep 9, 2010

I'm trying to LINQ two tables based on a dynamic key. User can change key via a combo box. Key may be money, string, double, int, etc. Currently I'm getting the data just fine, but without filtering out the doubles. I can filter the double in VB, but it's slooooow. I'd like to do it in the LINQ query right out of the gate.

[Code]...

"Range variable name can be inferred only from a simple or qualified name with no arguments."

View 3 Replies

Linq Join On Parametrized Distinct Key?

Jun 9, 2011

I'm trying to LINQ two tables based on a dynamic key. User can change key via a combo box. Key may be money, string, double, int, etc. Currently I'm getting the data just fine, but without filtering out the doubles. I can filter the double in VB, but it's slooooow. I'd like to do it in the LINQ query right out of the gate.

LinqMasterTable:
-------------------------------------------------------------
| AppleIndex | AppleCost | AppleColor | AppleDescription |
------------------------------------------------------------

[Code].....

View 1 Replies







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