Perform An Aggregation On A DataTable With Linq In .net
Dec 22, 2009
I wish to output an IEnumerable type (dictionary(of T) would be perfect, otherwise a datatable would be acceptable) that provides an aggregation of the data within the datatable.In SQL I would write the query as such: select groupByColumn, sum(someNumber) from myTable group by groupByColumn
In VB.NET the closest I have got to this (achieved using information here) is: dt is a datatable containing two columns, referred to by index in p below Dim q = From p In dt Group p By p(0) Into Sum(p(1)) Select p(0), SumOfNumber = Sum
However I receive error: "Range variable name can be inferred only from a simple or qualified name with no arguments." on the p(0) element. Therefore my question is as follows:How can I resolve this error? How do I process the result (q) as an IEnumerable type? Life isn't made any easier because I'm using Linq in unfamiliar vb.net. Many examples are in C#, but I haven't really come across anything suitable even in C#.
View 1 Replies
ADVERTISEMENT
Dec 19, 2009
How do I perform an sql query onto a DataTable not a Database table?
View 1 Replies
May 11, 2011
I want to know how to perform row delete to datatable which is a datasource to the grid.
In details: I have a datagridview (dg) and its datasource is set to dtItem. User will select some rows in dg and press 'delete' button.
dg.DataSource = dtItem;
I will perform a loop and delete the records. Here is the code eg:
foreach (DataGridViewRow drw in dg.Rows)
{
if (drw.Selected)
[Code]....
The problem is it didn't delete correctly. If user selects row index 0 and 1 in dg and delete it, it will delete the first one correctly and then finished cuz no more row is selected in next gridviewrow.
I tried google and didn't find what I want. may be because i don't know the correct keywords I should use to search.
View 2 Replies
Jun 9, 2010
I can achieve in VB what the following C# snippet does but it seems very clunky since I perform a Linq query to obtain the events for the relevant user. Is there a neat way?
ctx.FetchEventsForWhichCurrentUserIsRegistered((op) =>
{
if (!op.HasError)[code]........
View 2 Replies
Oct 4, 2011
Object aggregation of inherited classes
View 3 Replies
Aug 24, 2010
I have a situation in a program I'm writing where I'm not sure of the best way of coding a couple of objects and was hoping for a few pointers.I am using VB.NET 2003, so F/wrk v1.1. My business is horticultural in nature and I'm trying to help streamline the business with a new order fulfillment app.In my program, I have products. Some products don't have any component parts, but others are made up of a selection of plants.For example: we sell a trowel, which obviously doesn't have any component parts. But we also sell a large vegetable garden, which is made up of, say, 20 rocket plants, 30 salad plants, 5 spring onions, etc.[code]I believe the ProductContents table is technically referred to as a 'join table'?
I have written the classes in VB.NET which load, save, edit the Products and the Plants. I also have Manager classes which store collections of these objects (ProductMgr, PlantMgr).I have dealt with situations before with join tables where there are only two columns which are both FKs and both make up a composite PK (ie - to express a one-to-many relationship). This is pretty straightforward (using my product/plant tables as an example) because when I load an individual Product from the database, I also run a second SELECT query which returns any PlantIDs associated with that PlantID from ProductContents. I store that in an array of integers within the Product object. I can then load Plants from the PlantMgr object based on the PlantIDs stored in this array. I kind-of do the reverse when I save the Product to the database, I run a second INSERT query which updates the ProductContents table with the Plants associated with the Product.Where I'm getting stuck is with the inclusion of the extra 'Quantity' column. I can no longer store the PlantIDs in an array of Integers because I also need to store the Quantity associated with that Plant. I also can't use the PlantMgr object any more (to express which Plants are in each Product) because it also doesn't include any information about Quantity.I was just wondering if there is a standard way of handling this kind of situation, where I have a 'join table' which also includes extra columns as well as the two columns that identify the join.
View 1 Replies
Jun 24, 2011
I have the following SQL:
[Code]...
I want to put it (the select count statement) in this LINQ statement so I can get the sales count in my linq statement: Dim TheLeads = (From L In DB.Leads Where L.IsDeleted = False Select L).ToList() Is this possible to do in LINQ?
View 1 Replies
Feb 10, 2010
I'm trying to convert DataTable to Linq using
DIm result = From r in dt.AsEnumerable()
Select new ( col1 = r.Field<integer>("id"), col2 = r.Field<string>("desc"))
But i get error near 'new (' saying type expected.
View 2 Replies
Apr 16, 2009
I found the following example on [URL] Unfortunately, I need it in VB and it's using some constructs that neither I nor the automated code converters reciognize. Anyone out there know how this should be written in VB.Net? (the problem spot is the "select new {...")
PeopleDataSet ds = new PeopleDataSet();
using (PeopleDataContext context = new PeopleDataContext())
{
[Code].....
View 4 Replies
Apr 24, 2012
I need help converting datatable to xml using Linq. I could do it with hardcoded column names as you can see in my code .. but i need it without hardcoding it ..Example datatable ..My linq query ..
Dim xmlDoc As New XDocument(
From row In dt.AsEnumerable()
From row In dt.AsEnumerable()[code]......
View 1 Replies
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
Jan 23, 2009
Why does the following code give an error?
[Code]...
If I change the code to remove the second field, as below, then the code does not give an error.
[Code]...
View 5 Replies
Sep 21, 2010
discovered the compact mode of this instructions vs usual code. I found sample code for selecting, etc. from a datatable, transforming a column in a list or array - all operations are "Select"s - readonly.I need to make an simple update on a certain rows (using LINQ) from a DISCONNECTED datatable for which now I'm using this
for each dr as datarow in dtable.Rows
dr("MyField") = 77
next
[code].....
View 16 Replies
Jan 13, 2011
I have two DataTables:
dt1 - personid, name
dt2 - personid
I want to create a third datatable to include records from dt1 when they are NOT in dt2 using LINQ. In this case, I can bind the third datatable to a dropdownlist.
View 1 Replies
Jan 21, 2011
Am new to the concept Linq.I created a var where i joined 3 tables to get the data and then bind it in a datatable and create a csv file. Now i want to use an additioanl table in the same variable to be bind with the datatable but my problem is i cant use join here since i dnt have any common col or primary col in this additional table.How do i add this extra table?
[Code]...
View 2 Replies
Apr 20, 2009
I'm writing a small web app in VB.NET and I would like to do paging for my DataTable in the following function :
Public Shared Function Testing(ByVal KeyWord As String, ByVal CurrentPage As Integer, ByVal PageSize As Integer) As DataTable
Dim db As New BibleDataClassesDataContext
[code].....
View 1 Replies
Jan 24, 2012
Currently, I am using a loop to find the row I want to update.
Public Sub pubsub_SetUserDGVColumnWidth(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs)
' update all user settings for the given column
[Code]....
View 8 Replies
Jan 7, 2012
I have a Dynamic DataTable Created at runtime. The Setup is like so
Id | Name | Age
-----------------
3 | Mike | 21
6 | John | 43
8 | Sara | 34
What I am trying to do is come up with a linq statement I could use to find and update specific rows.
Such as a statement to change AGE to '33' WHERE ID = '3'
My code So far is:
-[VB.NET]-
Dim MyRow As DataRow = From column In MyTable.Rows Where column("Id") = 3
MyRow(0)("Age") = 33
But this is not updating my DataTable entry.
View 2 Replies
Jul 8, 2010
i want to use linq to fetch data from datatable.I have 2 tables in Cache and i want to join these tables then get value from resultset.I pasted Original Sql query and my linq query here.My linq query returns nothing.Where do i miss?My original sql query returns 1 row,but q1 is nothing
[Code].....
View 1 Replies
Aug 2, 2011
I write the code below. I want to make short using linq count.
Dim query0 = From obj In dtAginglist _
Where (obj.Field(Of String)("CurrentStatus") = "Open" Or obj.Field(Of String)("CurrentStatus") = "Acknowledge") _
[Code]....
I try to use code below but it dosen't work with if source is datatable.
Dim orderCounts = From c In customers New With { _
c.CustomerID, Key .OrderCount = c.Orders.Count() }
View 1 Replies
Feb 21, 2012
syntax of the LINQ query to use GROUP something BY something and select some fields of the datatable. i am having a datatable filled with some rows, i am trying to fill the datagridview with the GROUP BY query output of some fields.
View 4 Replies
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
Jul 20, 2010
How can i write this query with LINQ to a FoxPro database?SELECT count(*) FROM Table group by item1I wrote it as below, but it doesn't work
Dim Query
Dim dt As New DataTable
Dim da = New Odbc.OdbcDataAdapter("SELECT * FROM table1",connection)
[code].....
View 3 Replies
Nov 3, 2009
I am looking for sample linq code snippet which uses System.Linq.Dynamic against a datatable.
Dim entities = (From ent In dt.AsEnumerable().Where(String.Format("IsUSFederal == {0}", "true")) _
Select Description = ent("Description"), Acronym = ent("Acronym")).ToList
I am getting an error "there is no accessible Where can be called with these arguments". I have included the DynamicLinq.vb file and the application compiles fine (aside from this error). I have included Imports System.Linq.Dynamic but it doesn't appear to be working.
View 1 Replies
Feb 18, 2010
I have an SQL table(for example, it have 1 column "kod" with 3 rows:1,2,3 ). To operate it, I used class LINQ to SQL and I create tmp dataset with one table, with one column "col" with values - 2,3,4. To operate with this tmp table i use IEnumerable(T).I want to find only new values in tmp table and write its in SQL table. To find this values, i want to use LINQ query. Queris to each table are worked well. But query for 2 tables don't work.
Imports System.Linq
Imports System.Data.Linq
Public Class Form1
[code]....
View 5 Replies
Mar 2, 2012
I am trying to convert my EnumerableRowCollection into a DataTable, but I can't see the CopyToDataTable method. The enm.CopyToDataTable() has the blue saw tooth line under it with error saying " 'CopyToDataTable' is not a member of 'System.Data.EnumerableRowCollection' ". I have set a Reference to System.Data, System.Data.Linq, and System.Data.DataSetExtensions. I thought the CopyToDataTable method was part of the System.Data.DataSetExtensions, right? Anyone know why I can't access this method?
[Code]...
View 7 Replies
Sep 21, 2010
If txtSearchString.Text.Trim <> "" Then
Dim searchString As String = txtSearchString.Text.Trim
Dim results As EnumerableRowCollection(Of DataRow) = From PO In FilterPurchaseOrders().AsEnumerable
[CODE]..............
I am getting this error: error BC30205: End of statement expected.
FilterPurchaseOrders() function returns a datatable.
Whats wrong with the above code?
View 2 Replies
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
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
Apr 30, 2009
from a performance point of view is it faster to work with linq to sql or dataset.datatable?
View 6 Replies