Group by and order by - vb.net

using LINQ to NHibernate does anybody know how to use group by and order by in the same expression. I am having to execute the group by into a list and then order this, seem that I am missing soemthing here ???
Example:-
Private function LoadStats(...) ...
Dim StatRepos As DataAccess.StatsExtraction_vwRepository = New DataAccess.StatsExtraction_vwRepository
return (From x In StatRepos.GetAnswers(Question, Questionnaire) _
Group x By xData = x.Data Into Count() _
Select New ChartData
With {.TheData = xData,
.TheValue = xData.Count}
).ToList.OrderBy(Function(x) x.TheData)
End Sub

I'm not a Visual Basic expert, but you should be able to use alias in the Select clause to create a temporary variable, so that you can refer to it in a later clauses such as Order By. Unless there is some limitation in NHibernate, the following should work:
return From x In StatRepos.GetAnswers(Question, Questionnaire) _
Group x By xData = x.Data Into Count() _
Select res = New ChartData With _
{ .TheData = xData, _
.TheValue = xData.Count } _
Order By res.TheData _
Select res
I guess that in some situations, this may be more readable than moving the Order By clause before the Select clause.

Related

Linq query to display unique set of names for current data set

I have a linq query that is used to display the list of requests from multiple users.And a Requestor can have multple requests(So the grid can have same Requestor multiple times). Now i am creating a dropdown list with unique Requestor that are displayed on the grid.(issue is i am not getting the distinct list but getting all the Requestor multiple times). Below is the linq query i am unsing.Can anyone suggest with correct linq query.
Dim rqstQry = From x In db.Request_vws _
Order By x.RequestID Descending _
Select x.RequestID,
Descr = x.Descr, _
RequestorName = String.Format("{0} {1}", x.FIRST_NAME, x.LAST_NAME), _
RelatedTask = GetTaskDescr(x.WorkID, x.TaskLabel, x.TaskDescr), _
RequestDescr = GetRequestDescr(x.RequestType), x.SubmitDttm, x.UpdatedDttm, _
x.ChangeDttm, _
x.MigrTimeStr, x.MigrApptTime, _
x.Requestor Ditinct
DataBind:
RequestorCB1.DataSource = rqstQry
RequestorCB1.DataTextField = "Requestor" RequestorCB1.DataValueField = "REquestor"
RequestorCB1.DataBind()
Need distinct user in the dropdownlist
Put parentheses around the LINQ query and append .Distinct()
Dim rqstQry = (From x In db.Request_vws _
Order By x.user
Select x.user).Distinct()
If you are including Request stuff in the result, then you cannot get distinct users (as Gert Arnold points out in his comment). Only include columns related to users.
If you still need information on requests then you must limit this information to one record per user. You would use a group by and use an aggregate in order to select a request (the first one, the last one etc.).
Got this by using the below query.
Dim rqstQry = (From x In db.Request_vws _
Order By x.RequestID Descending _
Select x.RequestID,
Descr = x.Descr, _
RequestorName = String.Format("{0} {1}", x.FIRST_NAME, x.LAST_NAME), _
RelatedTask = GetTaskDescr(x.WorkID, x.TaskLabel, x.TaskDescr), _
RequestDescr = GetRequestDescr(x.RequestType), x.SubmitDttm, x.UpdatedDttm, _
x.ChangeDttm, _
x.MigrTimeStr, x.MigrApptTime, _
x.Requestor). Distinct()
Dim rqstQry2 = (From y In rqstQry _
Select y.Requestor, y.RequestorName).Distinct()

linq using order by, where, and select vb.net

I have a Linq query that I am passing to a list, and then to the view through the viewbag. I am trying to keep that list in a specific order, so that when I iterate through it I have control over the order in which it's displayed.
Here is the query:
ViewBag.attributes = (From row In db.tblCategory_Attributes
Where row.Item_Type_Identifier = itemType
Order By row.Category_Attribute_Identifier
Select CStr(row.Attribute_Name)
Distinct).ToList()
I am successfully passing this list to the view and iterating through it, but no matter what the values are always displayed in alphabetical order. Category_Attribute_Identifier is an integer that aligns with the order I would like these values to be displayed in.
I've played around with the order of my statements quite a bit and I'm not having any luck.
Can you tell me how to distinctly select the Attribute_Name's that correlate with my specific Item_Type_Identifier and order my results by the Category_Attribute_Identifier?
The Distinct is creating its own ordering again (because it shuffles through the result to filter out duplicates). Just do the sorting after the Distinct:
(From row In db.tblCategory_Attributes
Where row.Item_Type_Identifier = itemType
Select row
Distinct)
.OrderBy(Function(row) row.Category_Attribute_Identifier)
.Select(Function(row) CStr(row.Attribute_Name))
Try using Group By instead of Distinct
ViewBag.attributes = (From row In db.tblCategory_Attributes _
Where row.Item_Type_Identifier = itemType _
Order By row.Category_Attribute_Identifier) _
.AsEnumerable() _
.GroupBy(Function(r) r.Attribute_Name) _
.Select(Function(g) g.Key) _
.ToList()
Or use the extension method syntax which gives you the freedom of applying the extension methods in any order:
ViewBag.attributes = db.tblCategory_Attributes _
.Where(Function(row) row.Item_Type_Identifier = itemType) _
.Select(Function(row) New With {row.Attribute_Name, row.Category_Attribute_Identifier}) _
.Distinct() _
.OrderBy(Function(a) a.Category_Attribute_Identifier) _
.Select(Function(a) a.Attribute_Name) _
.ToList()
This simple test demonstrates that GroupBy preserves the order:
Public Shared Sub TestGroupOrder()
Dim a = New Integer() {6, 2, 4, 2, 7, 5, 3, 4}
Dim query = a.GroupBy(Function(i) i).[Select](Function(g) g.Key)
For Each i As Integer In query
Console.Write("{0} ", i)
Next
End Sub
Result in the console:
6 2 4 7 5 3

LINQ Join between datatables, two untyped fields

I'm querying two databases and trying to join the result sets and query out of them using LINQ. It seems it would be an easy task, but without doing an explicit join I'm having major performance issues. When I do the explicit join, I am having trouble with VB syntax for making things explicit types. Working Code, Cleaned:
For Each CurrRow In ResultsA.Tables(15).Rows
CurrDate = CurrRow("Date")
CurrID = CurrRow("ID")
CurrVal = CurrRow("Val")
Dim ResultsB = From SMW In DataSetA.Tables(0).AsEnumerable() _
Where SMW("ID") = CurrScheduleID And SMW("Time") = CurrProfileDate _
Select UTC_TS = SMW("Time"), Value = (SMW("VALUE") / 1000), Time_Zone = SMW("Time_Zone"), ID = SMW("ID")
Dim CurrentResult As Object
Dim boolSchedDateFound As Boolean = False
For Each CurrentResult In ResultsB
If CurrentResult.Value <> CurrVal Then
'LogIntegrityCheckErrorRow()
End If
boolSchedDateFound = True
Next
Next
This takes FOREVER to run with 100,000 rows.
I've been trying to rewrite this as:
Dim MismatchRows = From TableAData In DataSetA.Tables(0).AsEnumerable() Join TableBData In DataSetB.Tables(15).AsEnumerable() _
On New With {.TableAID = Convert.ToInt32(TableAData("ID")), .TableATime = Convert.ToDateTime(TableAData("Date"))} _
Equals New With {.TableBDID = Convert.ToInt32(TableBData("ID")), .TableBTime = Convert.ToDateTime(TableBData("Time"))} _
Select .................. (Hard to clean up, but this isn't the part that's failing)
And I'm having a bear of a time with it. The fundamental problem is the lack of strong typing. I've looked, but there seems to be little advice because most people doing this build EF on the data. Which isn't a terrible idea, but would require a bunch of re-engineering. So. Problem in front of me, how do I remove the error:
'Equals' cannot compare a value of type '<anonymous type> (line 2641)' with a value of type '<anonymous type> (line 2642)'.
Thank you so much for your help.
Have you tried this?
Dim MismatchRows = From TableAData In ei _
Join TableBData In e2 On _
TableAData("ID") Equals TableBData("ID") And TableAData("Data") Equals TableBData("Time")
Select .......
db.tb_DeviceGeFenceDetail.Join(db.tb_InventoryLog, Function(gfd) gfd.DeviceID, Function(il) il.deviceId, Function(gfd, il) New From { _
gfd.GeFenceLat1, _
gfd.GeFenceLng1, _
gfd.GeFenceLat2, _
gfd.GeFenceLng2, _
il.deviceName, _
il.DeviceIcon, _
gfd.DeviceID, _
il.id _
}).ToList().Where(Function(y) intValues.Contains(y.id))
you can try joining tables something like this.

New to Linq; need to grab multiple values from single row

I'm trying to retrieve multiple columns from a datatable, but only from a single row -- and then set properties based on those results. I've figured out how to run multiple queries to obtain single columns at a time, but there must be a way to combine it all into one query.
Here's what I thought might work:
Dim colSettingsQry = From r In Me.GridProcColumnSettings.AsEnumerable _
Where r("DataFieldNm") = colNm _
Select New With _
{ _
.uniqueNm = r.Field(Of String)("UniqueNm").Single(), _
.sortExpression = r.Field(Of String)("SortExpression").Single(), _
.headerTxt = r.Field(Of String)("HeaderTxt").Single(), _
.headerStyleWidth = r.Field(Of String)("HeaderStyleWidth").Single(), _
.dataFormatString = r.Field(Of String)("DataFormatTxt").Single() _
}
gridCol.SortExpression = From c In colSettingsQry _
Select c.sortExpression
gridCol.HeaderText = From c In colSettingsQry _
Select c.headerTxt
... etc.
I'm guessing there's something pretty obvious that I'm missing - anyone have suggestions?
Thanks in advance.
I think you're looking for this:
Dim colSettingsQry = ... (your query)
Dim setting = colSettingsQry.FirstOrDefault()
If setting IsNot Nothing Then
gridCol.SortExpression = setting.SortExpression
gridCol.HeaderText = setting.HeaderText
...
EndIf
By FirstOrDefault you take the first element of a sequence if there is any, else Nothing.

How to select replicated items in a list with linq?

Hy Guys!
I have a list of "ObjDay" which could have replicated items. How can I select them?
ObjDay is:
Public class ObjDay
_date
...
End Class
I tried to do:
replicatedItems = From d In ObjDaysList _
Order By d._date _
Group d By d._date Into newGroupOfObjDays = Group _
Where newGroupOfObjDays.Count > 1 _
Select newGroupOfObjDays
But I'm getting this error:
Can't convert object of type 'WhereSelectEnumerableIterator2[VB$AnonymousType_02[System.DateTime,System.Collections.Generic.IEnumerable1[ObjDay]], System.Collections.Generic.IEnumerable1[ObjDay]]'
in type 'System.Collections.Generic.List`1[ObjDay]'.
Tks for help!
You're selecting groups, and trying to assign it to a List(Of ObjDay). That won't work. You could do:
'Get groups of duplicate items
Dim groups = From d In ObjDaysList _
Order By d._date _
Group d By d._date Into newGroupOfObjDays = Group _
Where newGroupOfObjDays.Count > 1 _
Select newGroupOfObjDays
'Get all items from these groups with SelectMany
replicatedItems = groups.SelectMany(Function(item) item).ToList