How to union two LINQ queries (WhereSelectEnumerableIterator) error - vb.net

I am trying to build aKendo bar chart. I need the number of tickets opened, and tickets closed. I need that result grouped by month. Here is my LINQ
Dim openTickets = (From t In queue _
Where _
(t.CreateDate.Year = Convert.ToDateTime(DateTime.Now).Year)
Group t By _
ID = CType(t.CreateDate.Month, Integer), _
Month = CType(t.CreateDate.ToString("MMMM"), String) _
Into g = Group _
Select New With _
{.Month = Month.Substring(0, 3), .Opened = g.Where(Function(t) t.CreateDate.Month = ID).Count(Function(t) t.Id)})
Dim closedTickets = (From t In queue _
Where _
(t.CloseDate.Year = Convert.ToDateTime(DateTime.Now).Year)
Group t By _
ID = CType(t.CloseDate.Month, Integer), _
Month = CType(t.CloseDate.ToString("MMMM"), String) _
Into g = Group _
Select New With _
{.Month = Month.Substring(0, 3), .Closed = g.Where(Function(t) t.CloseDate.Month = ID).Count(Function(t) t.Id)})
Dim ticketCount = openTickets.Union(closedTickets)
When I try this, I get "WhereSelectEnumerableIterator". If I change the second query so that the name is ".Opened" and not ".Closed" it works, but then I do not know my count for "closed".
Ultimately I am trying to get an output of an array to supply the chart... similar to this:
[{"Month":"Apr","Opened":138,"Closed":150}
INSTEAD OF
[{"Month":"Apr","Opened":138,"Closed":0},{"Month":"Apr","Opened":0,"Closed":150}

You need to use a Join statement to merge the two and keep the values distinct. Something like this:
Dim tickets = From open In openTickets _
Join closed In closedTickets _
On open.Month Equals closed.Month _
Select New With _
{.Month = open.Month, .Opened = open.Opened, .Closed = closed.Closed}

Related

Linq query to array

This is currently my Linq query:
result = From d In dc.Tracker
Where d.Responsible.Value = "first last name"
Order By d.Priority1, d.Deadline
Select New With { _
Key .Title = d.Name, _
Key .Description = d.Priority1, _
Key .Priority2= d.Priority2, _
Key .Status = d.Status, _
Key .Resp= d.Resp, _
Key .Deadline = d.Deadline, _
Key .Notes = d.Notes, _
}
I am trying to output the data but can only seem to do so doing this:
For Each d In result
Console.WriteLine(d)
Next
But i can not seem to place it into an array so that i can call it like this:
result(0), result(1), etc etc...
Or even better:
result.Title, result.Description, etc etc...
How can i change that linq into an array?
Use the .ToArray() method.
result = (From d In dc.Tracker
Where d.Responsible.Value = "first last name"
Order By d.Priority1, d.Deadline
Select New With { _
.Title = d.Name, _
.Description = d.Priority1, _
.Priority2= d.Priority2, _
.Status = d.Status, _
.Resp= d.Resp, _
.Deadline = d.Deadline, _
.Notes = d.Notes}).ToArray()
Got it working at least how i am needing it too:
Dim Title As New ArrayList
Dim Description As New ArrayList
Dim Priority2 As New ArrayList
Dim Status As New ArrayList
Dim Resp As New ArrayList
Dim Deadline As New ArrayList
Dim Notes As New ArrayList
result = From d In dc.Tracker
Where d.Responsible.Value = "first last name"
Order By d.Priority1, d.Deadline
Select New With { _
Key .Title = d.Name, _
Key .Description = d.Priority1, _
Key .Priority2= d.Priority2, _
Key .Status = d.Status, _
Key .Resp= d.Resp, _
Key .Deadline = d.Deadline, _
Key .Notes = d.Notes, _
}
For Each d In result
If Not IsNothing(d.Title()) Then Title.Add(d.Title()) Else Title.Add("NA")
If Not IsNothing(d.Description()) Then Description.Add(d.Description().value) Else Description.Add("NA")
If Not IsNothing(d.Priority2()) Then Cost.Add(d.Priority2().value) Else Priority2.Add("NA")
If Not IsNothing(d.Status()) Then Status.Add(d.Status().value) Else Status.Add("NA")
If Not IsNothing(d.Resp()) Then Responsible.Add(d.Resp().value) Else Resp.Add("NA")
If Not IsNothing(d.Deadline()) Then Deadline.Add(d.Deadline()) Else Deadline.Add("NA")
If Not IsNothing(d.Notes()) Then Notes.Add(d.Notes()) Else Notes.Add("NA")
Next

Group By LINQ Dyanmic Query

I am trying to allow the user to change the query dynamically by a querystring parameter. I am having trouble getting the group by to work in vb.net. I am wanting to return the id, name, and count of each group.
' load all of the types
Dim baseQuery = db.Answers _
.Where(Function(a) a.Question.CorrectAnswer <> a.answer) _
.Where(Function(a) a.Question.TypeId = TypeId)
If catId <> 0 Then
baseQuery = baseQuery _
.Where(Function(x) x.Question.CategoryId = catId) _
.ToList()
End If
If approachId <> 0 Then
baseQuery = baseQuery _
.Where(Function(x) x.ApproachId = approachId) _
.ToList()
End If
Dim grouppedQuestionTypesincorrectTotal = baseQuery _
.GroupBy(Function(x) x.Question.TypeId) _
.Select(Function(x) New With {.id = x.id}) _
.ToList()
Dim grouppedCategorieincorrectTotal = baseQuery _
.GroupBy(Function(x) x.Question.CategoryId) _
.ToList()
Dim grouppedApproachesincorrectTotal = baseQuery _
.GroupBy(Function(x) x.ApproachId) _
.ToList()
ViewBag.QuestionTypes = grouppedQuestionTypesincorrectTotal
ViewBag.Categories = grouppedCategorieincorrectTotal
ViewBag.Approaches = grouppedApproachesincorrectTotal

Linq Select with Where based on Dropdown values

I have a search page that has a series of 3 dropdowns that the user can use to limit the search results. Each of the dropdowns has an initial value of ALL. If any have a value other than ALL I need to include that as a WHERE statement.
This is where I become stuck. My Linq Select is...
Dim myComponents = From searchComponents In dc.Components _
Where searchComponents.Type = ddl_Type.SelectedValue _
AndAlso searchComponents.Size = ddl_Size.SelectedValue _
AndAlso searchComponents.WR = ddl_WR.SelectedValue _
Select searchCompnents)
At the moment my search will include all ddl selectedValues. I need to remove any that have a value of ALL. I hope I've explained correctly. Example if ddl_Size.SelectedValue = "ALL" then my statement would be...
Dim myComponents = From searchComponents In dc.Components _
Where searchComponents.Type = ddl_Type.SelectedValue _
AndAlso searchComponents.WR = ddl_WR.SelectedValue _
Select searchCompnents)
How can I achieve this in code. Thanks
Dim myComponents = From searchComponents In dc.Components _
Where (ddl_Type.SelectedValue = "ALL" OrElse searchComponents.Type = ddl_Type.SelectedValue) _
AndAlso (ddl_Size.SelectedValue = "ALL" OrElse searchComponents.Size = ddl_Size.SelectedValue) _
AndAlso (ddl_WR.SelectedValue = "ALL" OrElse searchComponents.WR = ddl_WR.SelectedValue) _
Select searchCompnents)

'Order by' ignored in LINQ query

I am trying to sort a list by company name. I have tried the following code but this sorts the list by CompID and not CoShort. How should I change this to sort by CoShort?
Public Shared Function [SelectCompanyData](iElement() As Integer) As List(Of CompanyList)
Dim db As New EntryDataContext()
Dim q As IQueryable(Of CompanyList) = (From Act_Sub_Manfu_Link In db.Act_Sub_Manfu_Links _
Join Company In db.Companies _
On Act_Sub_Manfu_Link.CompID Equals Company.CompID _
Where iElement.Contains(Act_Sub_Manfu_Link.ACCN) _
And Company.In_Dir _
Select New CompanyList With { _
.CompID = Company.CompID, _
.InDir = Company.In_Dir, _
.CoShort = Company.CoShort _
}).Distinct
q.OrderBy(Function(c) c.CoShort)
Dim list As List(Of CompanyList) = q.ToList
Return list
End Function
You have to assign ordered collection into a variable:
Dim oq As IOrderedQueryable(Of CompanyList) = q.OrderBy(Function(c) c.CoShort)
And use it to get List of results:
Dim list As List(Of CompanyList) = oq.ToList()
Doesn't need to be assigned to anything but the return value
Public Function SelectCompanyData(iElement() As Integer) As List(Of CompanyList)
Dim db As New EntryDataContext()
Return (From Act_Sub_Manfu_Link In db.Act_Sub_Manfu_Links _
Join Company In db.Companies _
On Act_Sub_Manfu_Link.CompID Equals Company.CompID _
Where iElement.Contains(Act_Sub_Manfu_Link.ACCN) _
And Company.In_Dir _
Select New CompanyList With { _
.CompID = Company.CompID, _
.InDir = Company.In_Dir, _
.CoShort = Company.CoShort _
}).Distinct().OrderBy(Function(c) c.CoShort).ToList()
End Function

LINQ OrderBy sort Problem

I have a LINQ Query:
bikersList = (From c In ngBikersDataContext.Reg_Bikers _
Order By c.L_Name _
Select New Bikers() With { _
.BikerID = c.BikerID, _
.F_Name = c.F_Name, _
.M_Name = c.M_Name, _
.L_Name = c.L_Name, _
.MyID = c.MyID, _
.Site = c.Site.GetValueOrDefault, _
.bk_Building = c.bk_Building, _
.bk_City = c.bk_City, _
.bk_Zip = c.bk_Zip.GetValueOrDefault, _
.bk_Phone = c.bk_phone, _
.email = c.email, _
.DeptZone = c.DeptZone, _
.QuartID = c.QuartID.GetValueOrDefault, _
.BikerDays = c.BikerDays.GetValueOrDefault, _
.BikerMiles = c.BikerMiles.GetValueOrDefault, _
.BikerTime = c.BikerTime.GetValueOrDefault, _
.BKLockID = c.BKLockID.GetValueOrDefault, _
.bk_Start_DT = c.bk_Start_DT, _
.bk_End_DT = c.bk_End_DT, _
.bk_Quarter = c.bk_Quarter.GetValueOrDefault, _
.bk_Year = c.bk_Year.GetValueOrDefault, _
.bk_Comments = c.bk_Comments, _
.IsActive = c.IsActive.GetValueOrDefault _
}).ToList()
This works great and sorts on L_Name. But I am trying to allow the user to sort the gridview themselves. So I am passing in the SortExpression as a string. But I don't know how to incorperate the SortExpression into the LINQ Query.
I tried
Order By c. & SortExpression
But that did not work.
You should check out something called a Dynamic Query in Linq.
Using the LINQ Dynamic Query Library
Here's an article that talks about dynamic sorting with linq using a sortexpression string:
http://www.codeproject.com/KB/recipes/Generic_Sorting.aspx
Basically, you'll need to build the expression tree manually.
(this code is from the link above)
Public Function Sort(ByVal source As IEnumerable(Of T), _
ByVal sortBy As String, _
ByVal sortDirection As String) As IEnumerable(Of T)
Dim param = Expression.Parameter(GetType(T), "item")
Dim sortExpression = Expression.Lambda(Of Func(Of T, Object))_
(Expression.Convert(Expression.[Property](param, sortBy), _
GetType(Object)), param)
Select Case sortDirection.ToLower
Case "asc"
Return source.AsQueryable().OrderBy(sortExpression)
Case Else
Return source.AsQueryable().OrderByDescending(sortExpression)
End Select
End Function