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


ADVERTISEMENT

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

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

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

When Fill A Datatable Can Get Distinct Rows For A Specific Column

Oct 23, 2009

i fill a datatable in a typed dataset.. it has cities and states but I want to pull just distinct states and bind it to a control etc.. there is the filter, but i havent found a way to do distinct or group by. got to be a way without adding more datatables and filling them with data thats already there right?

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

.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

VS 2010 Select Distinct Values From A DataTable?

Sep 28, 2010

Is it possible to select distinct values from a DataTable?

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

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

Compare Two Datatable And Delete Some Rows By Linq?

Nov 18, 2010

there is two datatable at some datasetsone of them is tbl1 and fileds is (pcode,points) other table is tbl2 and fields is (pcode,stoped)"pcode" is equal at two tables

View 6 Replies

LINQ To DataTable, Finding Duplicate Rows?

Jul 5, 2011

Using the following DataTable:

Dim d As New DataTable()
d.Columns.Add("Product", GetType(System.String))
d.Columns.Add("Value", GetType(System.Double))

[code]....

I'm trying to use LINQ to identify if there are more than one of any type of product. In SQL, this would work something like:

SELECT Product FROM SOME_TABLE HAVING COUNT(*) > 1

I can't for the life of me work out how to do this in LINQ. I was following someone who did something like this:

Dim q = From row In d.AsEnumerable()
Group row By New { column1 = row("Product"), column2 = row("Value") }
Into grp

[code]....

But I get an error when I try to actually use 'q'. how I can make this work?

View 3 Replies

Xml - Can't Get Datatable.Select To Return Any Rows

Mar 17, 2012

I have the following code that loads an XML file into a datatable (I went this route because I do not know how to query XML directly). I want to use the Select method to return a row where "age = 72". However, I can't seem to get this to work. Also, if there is a better way to search through a datatable for specifc values that would not require iterating through the whole table to get the results

Imports System.Xml
Module Module1
Sub Main()

[Code]....

View 1 Replies

VS 2010 LINQ On A DataTable - Are Rows Returned As Reference

Feb 6, 2011

When I query a DataTable using LINQ, are the rows returned as references? Either way, is it possible to control this behaviour, so that rows are either returned as references or as new rows?My guess is that all objects are returned as references by default, since that seems to be the normal behaviour in most programming languages.

View 2 Replies

DataTable Select Statement - Returns No Rows?

Mar 7, 2012

The following VB line, where _DSversionInfo is a DataSet, returns no rows:
_DSversionInfo.Tables("VersionInfo").Select("FileID=88")
But inspection shows that the table contains rows with FileID's of 92, 93, 94, 90, 88, 89, 215, 216. The table columns are all of type string.

Further investigation showed that using the ID of 88, 215 and 216 will only return rows if the number is quoted.
i.e. _DSversionInfo.Tables("VersionInfo").Select("FileID='88'")
All other rows work regardless of whether the number is quoted or not. I understand that the numbers should be quoted just not why some work and others don't?

View 2 Replies

Return The Rows From Datatable Using Select Method?

Nov 22, 2011

I'm having the datatable with three columns, i need to return the rows in which the third column having the null values.

ie., I need to do like this below mentioned coding,

Dim rows As DataRow() = ds.Tables("Tablename").Select("col3 is null")

Is it possible? if no, anyone tell me the way to get the results.

View 1 Replies

Dynamic List From DataTable.Select Rows Collection?

Jun 6, 2011

I'm am creating a "pop-up" CheckedListBox over a button on my DataGridView. The values in the list are going to be filtered based on the value in column 0 of the DataGridView.I have already pulled in a "master" DataTable with two columns: ACODE and MATRIX.I want to call the following function and return an array of strings that I can use to build this CheckedListBox with. The line between "For Each r" and "Next" generates a "Number of indices is less than the number of dimensions of the indexed array". The number of items is variable in the array so I can't predefined it. I know I'm going down the wrong path.

Function StudyGroupSamples(ByVal sampleType As String) As String()
Dim thisArray() As String
Dim theseRows() As DataRow

[code]....

View 2 Replies

Distinct Query For Datatable

Oct 22, 2011

If I would connect to database I would run a query like this: "Select distinct Column1, column2 from tablename where somefield=somevalue order by column2". How can I run this query to a datatable? I know .select but that's not enough like dataset.datatable("name").select(filter,short). Still need distinct.

View 2 Replies

Returning Just The Distinct Rows From A Dataset

Jun 25, 2009

I have a strongly typed dataset that has rows with duplicate [sopnumbe] I am looping through them and displaying row data I want to only display the Distinct sopnumbe rows in my initial loop1.. but the sub loop uses the data out of all rows.. So I cant filter the actual dataset.

[Code]....

View 6 Replies

C# - How To Get Distinct Values From DataTable In DataSet

Dec 23, 2011

I am using strongly typed dataset and have many tables in that. The problem is now I want to filter data from
GetData()
function which has a query like
select * from table_name

How can I filter a particular table and distinct values from it. Also if I try to filter it return all column but rest have null values except the one I asked, so I cannot assign it as a datasource to a datagrid or combobox. How can I do this..

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

Get Distinct Values And Converting Back Into DataTable?

Mar 14, 2012

I have a "result" which is DataTable and I like to get the x distinct values(City) and convert it back to DataTable how do I do this the code below gives me an error

Dim query = (From x In results.AsEnumerable()
Select (x.Field(Of String)("City"))).Distinct().CopyToDataTable()

so what I want is records of distinct cities I can get that but the problem I am having is converting it back to Datable.

Edited:I am using "where" in this statement and it does convert to Table(works fine) but not on "select"

Dim results = (From myRow In ds.Tables(1).AsEnumerable()
Where (myRow.Field(Of String)("xxxx") = xxxx)
Select myRow).Distinct().CopyToDataTable()

View 1 Replies

Get Row Count Of Distinct Values In A DataTable Column?

Mar 13, 2012

I have a DataTable that has several hundred rows.I want to get a row count of distinct values in a particular column.For example, I have a DataTable of Product Orders, but I want to get the number of unique Customers.

I was using this code, but I can't rely on it because my binding source is filtered from time to time.If my binding source has filtered out all rows datatable.DefaultView returns 0 rows.

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







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