C# - LINQ Select From IEnumerable With Distinct/GroupBy And Sorting?

Oct 19, 2010

My particular example is fairly complex but I think the concept would apply equally to something like a logging system so I'll use that instead for ease of explanation. It's a ficticious example, please don't harp on or agonise over what is achitectually, programatically or morally wrong with the example itself

[Code]...

I don't want to 'cheat' and resort to a long-winded way of doing it when performance isn't critical here and I'm moderately confident it can be done in a single LINQ statement.

View 1 Replies


ADVERTISEMENT

.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

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

Select Distinct In Linq Query?

Jan 4, 2012

I've a collection with the data like this.[code]...

how can select the distinct data using linq?

View 2 Replies

Use Linq For Select Distinct Values?

Mar 28, 2012

I'm pretty new in Linq and the first problem I've is to select the Distinct values from a ObservableCollection( Of T)

I've tried with[code]...

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

Select Distinct Rows From Datatable In Linq

Jul 14, 2010

I am trying to get distinct rows based on multiple columns (attribute1_name, attribute2_name) and get datarows from datatable using Linq-to-Dataset.[code]How to do thin Linq-to-dataset?

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

.net - GroupBy In LINQ To DataSet?

Jul 14, 2011

Background: I'm importing data from a MySQL database into a SQL-Server database(for reports and later a SSAS-Cube). I want to normalize the data at the same time. I want to group repeating Ticket_IDs to one record in a table Contact with other useful informations and leave the rawdata in the sub-table ContactDetail(with foreignkey to Contact).Hence every record in Contact has a unique Ticket_ID.

I've decided to use strong typed datasets for the import. Now i'm wondering what's the best way to detect if i've already added a Ticket_ID. I could check for it in every loop(~100000 records) but i'm assuming that there is a better/faster way.

Simplified sample-data:

Ticket_ID ID fiContact
89442226 1 1
89442226 2 1[code].....

I will have a look if this is faster than the iterating approach with a HashSet to detect if the contact was already created.

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

.net - Linq Query Not Returning IEnumerable?

Jan 22, 2010

I have the follow Linq query that is in a web application that was converted from .NET 1.1 to 3.5:

dim objListOfFilteredDataRows = from datarows as datarow in objDataSet.tables(0).rows _
where datarows("SomeColumn") = SomeValue

I have the exact same query in an application that was created using .NET 3.5 and the query returns an IEnumerable. However the query in the converted application is returning:

{Name = "WhereEnumerableIterator`1" FullName = "System.Linq.Enumerable+WhereEnumerableIterator`1[[System.Data.DataRow, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}

**Edit: When I highlight the expression, the intellisense says that it doesn't know the type of objListOfFilteredDataRows and assumes its a type of "Object". Why is the type not infered in the converted 1.1 application but is infered in the "native" 3.5?**

What am I missing here? How do I convert the "WhereEnumeratorIterator`1 to an IEnumerable?

View 5 Replies

.net - LINQ - IEnumerable.Join On Anonymous Result Set?

May 28, 2010

I've long since built a way around this, but it still keeps bugging me... it doesnt help that my grasp of dynamic LINQ queries is still shakey.

For the example:

Parent has fields (ParentKey, ParentField)
Child has fields (ChildKey, ParentKey, ChildField)
Pet has fields (PetKey, ChildKey, PetField)

[Code].....

The above Join call doesnt work. I sort of understand why it doesnt work, but hopefully it'll show you how I tried to accomplish this task.

After all this was done I would have appended a Select to finish the job.

I tried it with the PredicateBuilder with little success. I might not know how to use it right but it felt like it wasnt gonna handle the joining.

View 1 Replies

IEnumerable.where In Linq How To Get Obj&return Value Of Othr Func

Apr 4, 2012

How to give another function's returned value in LINQ Ienumerable.

Private function GetFruitColor(fruit) as string
'It returns fruit color.
'If valid fruit and color not available, it returns ""(empty string)

[code].....

'Below code is wrong. but please suggest me how to correct it. My intention is, I want output collection(say dictionary) of each fruitname and its color(returned by other function call) for all the fruits which the GetFruitColor is not nothing(it can be empty or valid string).

Dim query = _
fruits.Where(Function(fruit) k= GetFruitColor(fruit) if not k is nothing select fruit, k)
End Sub

View 8 Replies

Creating A Reference To An Object In An IEnumerable Linq Query Would Be Really Slow?

Jun 18, 2009

Is there any particular reason why creating a reference to an object in an IEnumerable Linq query would be really slow? I have a Linq query list consisting of my own objects (count is about 200).I was doing a time-critical task, and this line (which was used a lot in a loop) took nearly a millisecond to execute:

Dim CurObject as MyClass = QueryResult(CurIndex)

View 2 Replies

Working With System.collection.generic.Ienumerable/XML Linq Queries?

Oct 22, 2009

This is really a continuation from my earlier post. I've never worked with this datatype/collection (system.collection.generic.Ienumerable) before so I'm struggling to declare the query globally so I can reference the results within other sub routines/functions. I also am struggling to convert it to other types.how I can convert the collection of xml elements/system.collection.generic.Ienumerable into an xmltextreader (I know I will have to change the query below as it select the value of the node and not the node itself) I imagine by converting the query result to system.IO.stream My first port of call was MSDN but I find the material very difficult to understand for this particular subject.

[Code]...

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

Implement The .Find Method When Binding A BindingSource To LINQ (IEnumerable) Result?

Mar 15, 2008

I'm binding a DataGridView to a BindingSource to a LINQ IEnumerable, I found that the BindingSource.Find method throws an exception because .Find isn't implemented in IEnumerable (and the BindingSource just passes the call to it's DataSource). I need to use BindingSource.Find to select/highlight a particular row in the DataGridView.

Is there a feasable way to extend my DataContext to support this behavior w/o breaking anything else? I expected to find the code out there already, but I've searched exaustively with no luck.Without it, I cannot "move" the BindingSource using .Position and that's a pretty common use of the BindingSource I think.

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

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

Distinct Values In LINQ With Anonymous Types

Jul 2, 2011

Supposing the referenced List below contains 2 elements:
Dim Countries = From c In List _
Select New With { .Country = c.Country, .CountryID = c.CountryID }

The code above returns
.Country=Spain .CountryID = 1
.Country=Spain .CountryID = 1

How can I get the distinct values? The Countries query should contain only
.Country=Spain .CountryID = 1

View 5 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 DataTable Not Producing Distinct Values?

Oct 19, 2010

I have a datatable which has been dynamically generated from FoxPro tables using a UNION Select statement. e.g.SELECT * FROM x UNION SELECT * FROM y UNION SELECT * FROM Z ORDER By v_alue1This produces a datatable with about 100 rows, each containing many fields, one of which is c_olor. From this datatable, I would like to select the distinct colors and then output in a dropdown.I have a public class Color which just has one property which I can then use as the DataTextField and DataValueField for the dropdownlist

Public Class Color
Private _c_olor As String
Public Property c_olor() As String

[code].....

View 1 Replies

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

Selecting Distinct Within LINQ Of Data Rows

Oct 24, 2011

I have a DataTable with about 64 columns, including "UserAnswer", "CorrectAnswer", and "QID_Lookup". I am building a LINQ that will select all the rows that have a correct answer (user answer matches correct answer), but I only want to select rows that have distinct "QID_Lookup".

[Code]...

The problem is that the result set includes duplicate QID_Lookup values. How can I include only distinct QID_Lookup values? keep in mind that there are 64 columns, so if I can avoid it, I wouldn't want to list each column individually for selection.

View 9 Replies







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