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.
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
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
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:
also i have a table called Orders with several columns, they contain information about the order and store the ID of the Customer they belong to.Question is: how can i query that table with (preferably) linq (the datacontext is from LinqToSql) to return the following dataI want to search for any entry with the matching CustomerID which took place, group them by Year, Sum the Totals respectively and add them to the listview?I now i could use lamda expressions and aggregate, its just not clear how (option infer on,db is a datacontext object,CustomerID is an int32 variable):
Dim Orders = (From order In db.Orders Where order.CustomerID = CustomerID).GroupBy(Function(p) p.Date.Year).GetEnumerator I reckon i'd have to create an anonymous type like the following:
I have the following query that groups parking spaces by their garage, but I can't figure out how to iterate the data in the view.
Public Function FindAllSpaces() Implements ISpaceRepository.FindAllSpaces Dim query = _ From s In db.spaces _ Order By s.name Ascending _
[code]....
The controller is taking the query object as is and putting it into the viewdata.model and as stated the view is not currently strongly typed as I haven't been able to figure out how to do this. I have run the query successfully in linqpad.
How do I order by and group by in a Linq query?I tried..Dim iPerson = From lqPersons In objPersons Where Len(lqPersons.Person) > 0 Group lqPersons By key = lqPersons.Name Into Group Order By Group descending Select Group, key
For Each i In iPerson tmp = tmp & vbNewLine & i.key & ", " & i.Group.Count Next
I am trying to do a simple summary of table data using a LINQ query.a regular SQL Query looks like:[code]This query works fine and returns the correct results.[code]This LINQ query returns the incorrect results some of the time. Off by .01 in most cases.The "value" column is of type Numeric (12,2) in the database.Now when I show the values from the Sum in a msgbox I get extra precision and thus incorrect results as my end result because of rounding.For example I get 835.18 + 54.62 = 889.80960 from the above LINQ query.I am running Visual Studio 2008 9.0.30729.1 SP on Vista 64Bit with .NET 3.5 SP1.
My LINQ query contains the following Group By statement:
Group p By Key = New With { _ .Latitude = p.Address.GeoLocations.FirstOrDefault(Function(g) New String() {"ADDRESS", "POINT"}.Contains(g.Granularity)).Latitude, _ .Longitude = p.Address.GeoLocations.FirstOrDefault(Function(g) New String() {"ADDRESS", "POINT"}.Contains(g.Granularity)).Longitude}
The query works, but here is the SQL that the clause above produces
SELECT [t6].[Latitude] FROM ( SELECT TOP (1) [t5].[Latitude]
[Code]....
but this produced an error: "A group by expression can only contain non-constant scalars that are comparable by the server."
I have the following SQL query to return all Customers who have no OrderLines with no Parts assigned - i.e. I only want the customers within which every order line of every order has no parts assigned - (in the actual problem I am dealing with a different domain but have translated to customers/orders to illustrate the problem)
SELECT c.Customer_PK FROM Customers c INNER JOIN Orders o[code].....
This works but generates less than optimal SQL - it is doing a subquery for Count on each row of the customers query rather than using Group By and Having. I tried making the LINQ Group By syntax work but it kept putting the filter as a WHERE not a HAVING clause.Edit in response to Answers below: I am accepting JamieSee's answer as it addresses the stated problem, even though it does not produce the GROUP BY HAVING query I originally had.I am a VB developer so I have had a crack translating your code to VB, this is the closest I got to but it does not quite produce the desired output:
Dim qry = From c In context.Customers Group Join o In context.Orders On c.Customer_PK Equals o.Customer_FK Into joinedOrders = Group[cod].....
The problem I had is that I have to push "jl" into the Group By "Key" so I can reference it from the Where clause, otherwise the compiler cannot see that variable or any of the other variables appearing before the Group By clause.With the filter as specified I get all customers where at least one order has lines with no parts rather than only customers with no parts in any order.
I have these objects:[code]Using LINQ I need to take a List(Of MakeInfo) and a List(Of ModelInfo) and aggregate the StockInfo from ModelInfo into the List(Of MakeInfo).So for each MakeInfo I will have a total count of all stock where MakeInfo.Name = ModelInfo.Make, and also a minimum price.I think it's going to be something like this, but I'm having trouble accessing the nested object and not sure if it's going to be possible with a single query.[code]
Dim qry As <??> = From f In dirInfo.GetFiles("*.QBW") Select File = f.FullName, Include = True Dim dt As DataTable = qry.CopyToDataTable()
I tried as "IEnumerable(Of DataRow)" but that didn't work. At runtime it said:
Unable to cast object of type 'WhereSelectArrayIterator2[System.IO.FileInfo,VB$AnonymousType_02[System.String,System.Boolean]]' to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataRow]'.
How to JOIN tables on nullable columns? I have following LINQ-query, RMA.fiCharge can be NULL: Dim query = From charge In Services.dsERP.ERP_Charge _ Join rma In Services.dsRMA.RMA _ On charge.idCharge Equals rma.fiCharge _ Where rma.IMEI = imei Select charge.idCharge
I get a "Conversion from type 'DBNull' to type 'Integer' is not valid" in query.ToArray(): Dim filter = _ String.Format(Services.dsERP.ERP_Charge.idChargeColumn.ColumnName & " IN({0})", String.Join(",", query.ToArray)) So I could append a WHERE RMA.fiCharge IS NOT NULL in the query. But how to do that in LINQ or is there another option?
The problem was that the DataSet does not support Nullable-Types but generates an InvalidCastException if you query any NULL-Values on an integer-column. The modified LINQ-query from dahlbyk works with little modification. The DataSet generates a boolean-property for every column with AllowDbNull=True, in this case IsfiChargeNull.
Dim query = From charge In Services.dsERP.ERP_Charge _ Join rma In (From rma In Services.dsRMA.RMA _ Where Not rma.IsfiChargeNull Select rma) On charge.idCharge Equals rma.fiCharge _ Where rma.IMEI = imei Select charge.idCharge
I'd like to take all suggestion either in C# or VB.NET.I have a DB diagram like the image below. I also include the database script here [URL]In the Students table, CountryId and RoomId column are allowed null. Because some records do not have info about room and country yet.Also, some students do not have essays.I'm doing a joint query with all tables. I want to select all students to project the result like this:Wanted query result.
Here's my current query that gives the result like the image below:
Dim db As New DBDataContext Dim query = From st In db.Students _ Join c In db.Countries On c.Id Equals st.Id _ Join r In db.Rooms On r.Id Equals st.RoomId _
[code].....
current query result I got only one result back because I have (2) William NoMan record in every table. But I don't get anything about others, like (3) Sync Master who has everything but RoomId.What do I need to modify the query above so it will give me all students like in the wanted query image above?
I have the below Linq query that is returning the data but I need to aggregate the columns to group on the Period and Sum the Count columns. How do I go about doing this?
LINQ from t In tblTimes join h In tblEngineeringDashboard_CADMachinesCounts on t.ID Equals h.TimeID Order By t.Period
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
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
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)()
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).
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.
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?
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."
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.
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.