I have tried the following to modify the sort order in order to have the fields with HeureDebut sorted to the end when it is 00:00 but it does not work
Is there another method to achieve this?
Dim theCompetitions As List(Of competitions) = (From aCompetition In context.competitions
Where aCompetition.concour_id = idConcours
And aCompetition.isPublic = 1
And aCompetition.idMYMaster = 0 _
Order By aCompetition.DateEpreuve, If(aCompetition.HeureDebut = "00:00", "99:99", aCompetition.HeureDebut), aCompetition.NoRef).ToList()
Related
Sorting:
Dim objReport(0) As stReport
Try
For intK = 1 To 15
objReport(intK).Name = "User" & GetRandomNumber()
objReport(intK).Age = intK
objReport(intK).Misc1 = "data"
Next
Return objReport
Is there any simple way to sort the objReport order by Name?
NOTE: The code is just a sample.
You can sort them using OrderBy Extension Function
objReport = objReport.OrderBy(Function(x) x.Name).ToArray()
How can I fuse these 2 If statements to make both my Droplist filters work together to filter the data? I have 2 droplists (transfilter and Soortfilter) and I want to be able to select something out of droplist 1 and select other thing out of droplist 2 and then press filter and have it show the items that have been selected by filtering with both filters.
# this point I have Return View(query.ToList()) what only shows my filter items from Droplist 1
Function Index(transFilter As TransactieType?, soortfilter As Soort?) As ActionResult
Dim query As IEnumerable(Of Pand)
If Not transFilter.HasValue OrElse transFilter.Value = TransactieType.Beiden Then
query = db.Panden
Else
query = db.Panden.Where(Function(p) p.TransactieType = transFilter.Value)
End If
If Not soortfilter.HasValue OrElse soortfilter.Value = Soort.All Then
query = db.Panden
Else
query = db.Panden.Where(Function(p) p.Soort = soortfilter.Value)
End If
Return View(query.ToList())
End Function
I was trying this but that didn't really work
Function Index(transFilter As TransactieType?, soortfilter As Soort?) As ActionResult
Dim query As IEnumerable(Of Pand)
If Not transFilter.HasValue And soortfilter.HasValue OrElse transFilter.Value = TransactieType.Beiden And soortfilter.Value = Soort.All Then
query = db.Panden
Else
query = db.Panden.Where(Function(p) p.TransactieType = transFilter.Value And p.Soort = soortfilter.Value)
End If
Return View(query.ToList())
The problem is that you're setting query = db.Panden in the first part of both If statements. When you're dealing with incrementally building up a LINQ query, always
Start with a base case. In this case it would be Dim query As IEnumerable(Of Pand)
Add each additional filter directly to that base case using query = query.Where.
Function Index(transFilter As TransactieType?, soortfilter As Soort?) As ActionResult
Dim query As IEnumerable(Of Pand) = db.Panden
If transFilter.HasValue AndAlso transFilter.Value <> TransactieType.Beiden Then
query = query.Where(Function(p) p.TransactieType = transFilter.Value)
End If
If soortfilter.HasValue AndAlso soortfilter.Value <> Soort.All Then
query = query.Where(Function(p) p.Soort = soortfilter.Value)
End If
Return View(query.ToList())
End Function
I am trying to convert following sql select * from tblPBRule where PBRuleId = 123 and StartDate < = '20140902' and finishdate is null and PBCodeid in (select PBCodeid from tblpbrule where PBHourstypeid IN (3,4,5,6)) into linq query within application
Dim query As IEnumerable(Of PBRuleData.PBRuleRow) = From s In Me.PBRule.PBRule.AsEnumerable() _
Where s.PBRuleId = .PBRuleId And s.StartDate <= .PBDate _
And ((s.IsFinishDateNull) OrElse (s.FinishDate > .PBDate)) Select s
can anyone help me to complete this query
Have just gone through the same search and found Brad's Blog which works for me as in my function below.
http://www.gorgando.com/blog/technology/asp-net/not-in-subquery-with-asp-net-linq-in-vb
Hope this helps. The only difference between the 2 queries of the second table is the "Where Not" for the not in condition.
Private Enum InOrNotInSelect
None = 0
InTable = 1
NotInTable = 2
End Enum
Private Function getNotInData(firstTable As DataTable,
secondTable As DataTable,
inOrNotIn As InOrNotInSelect,
keyField As String) As DataTable
' TODO: make keyField a ParamArray
' (don't know how to write dynamic LINQ code yet)
Dim loTable As DataTable = Nothing
' first of all do a query on the first table
Dim firstQuery = From dt1 In firstTable.AsEnumerable()
Select dt1.Field(Of String)(keyField)
If inOrNotIn = InOrNotInSelect.NotInTable Then
' next do a query on the second table returning the rows NOT IN the first table
Dim inNotInQuery = From dt2 In secondTable.AsEnumerable()
Where Not firstQuery.Contains(dt2.Field(Of String)(keyField))
Select dt2
' convert the rows to a table
loTable = inNotInQuery.CopyToDataTable()
ElseIf inOrNotIn = InOrNotInSelect.InTable Then
' next do a query on the second table returning the rows IN the first table
Dim inNotInQuery = From dt2 In secondTable.AsEnumerable()
Where firstQuery.Contains(dt2.Field(Of String)(keyField))
Select dt2
' convert the rows to a table
loTable = inNotInQuery.CopyToDataTable()
End If
' remove debug code in production build
For Each dRow As DataRow In loTable.Rows
Debug.Print(dRow(keyField) & " - " & dRow("FullName"))
Next
Return loTable
End Function
Dim x = From row In f_table.AsEnumerable()
Select row("Crop")
From what I understand, the "f_table.AsEnumerable" should make my search object ("row" in this case) a datarow object. This simple example runs without any exceptions but does not find any entries (This search works if I switch to an array of datarows that have been taken from f_table, in place of f_table.AsEnumerable).
Any ideas why AsEnumerable is not allowing for searching the rows of the table?
edited/added: The following is what I have, where "emptyrows" is a subset array of rows from f_table.
Dim emptyrows_grouped = From row In emptyrows
Order By row("Date"), row("Time")
Group By New With {.date = row("Date")}.date,
New With {.crop = row("Crop")}.crop
Into Group
What i want is this form:
Dim emptyrows_grouped = From row In f_table.AsEnumerable
Where row.Field(Of String)("SamplePosition") Like "Emp%"
Order By row("Date"), row("Time")
Group By New With {.date = row("Date")}.date,
New With {.crop = row("Crop")}.crop
Into Group
It works like this:
Dim query = dt.AsEnumerable
.Where(Function(dr) dr("column name").ToString = "something").ToList
This yields a List of DataRows where this column has the value of "something"
GroupBy:
Dim query = dt.AsEnumerable
.Where(Function(dr) dr("column name").ToString = "something")
.GroupBy(Function(dr) dr("column name"))
Never mind - I'm a gigantic fool today because f_table is the wrong datatable. I used the right one and it worked.
Dim emptyrows_grouped = From row In file_table.AsEnumerable
Where row.Field(Of String)("SamplePosition") ="Empty"
Order By row("Date"), row("Time")
Group By New With {.date = row("Date")}.date,
New With {.crop = row("Crop")}.crop
Into Group
Please excuse my wasting of your time!!
I am trying to work out a LINQ query to pull items from a List, the List contains a child-nested List - which I need to query for an item, then use the resulting item in my final sort of all records returned the query.
The parent is List(Of MediaItems) - the class structure as follows:
MediaItems
.ID (int)
.Src (string)
.Advert (bool)
.AdOptions As List(Of AdvertOptions)
.Counter (int)
AdvertOptions class consists of:
.Age (int)
.Gender (int)
.Priority (int)
I want to query for any MediaItems that meet the following criteria:
.Advert = true
.Age = x (paramter in calling function)
.Gender = y (paramter in calling function)
To do this, I am using the following LINQ query: (Age and Gender are the function parameters)
Where(Function(s) s.Advert And s.AdOptions.Any(Function(a) a.Gender = Gender And a.Age = Age))
I now need to sort the results based on two sorting levels:
AdOptions.Priority (in descending order), then sort by Counter in ascending order
This is my failing attempt:
Where(Function(s) s.Advert And s.AdOptions.Any(Function(a) a.Gender = Gender And a.Age = Age)).OrderBy(Function(a) From p1 In a.AdOptions Where p1.Gender = Gender And p1.Age = Age Select p1.Priority).ThenByDescending(Function(s) s.Counter)
My attempt is not working, I am getting the following error:
at least on object must implement IComparable
Can anyone see what needs to be done to achieve my goal?
Ben
On reading your code a bit closer, I spotted that you order by AdOptions
I think this is what you really want
Sub Demo(ByVal gender As Integer, ByVal age As Integer)
Dim items = New List(Of MediaItem)()
Dim matching = Function(a) a.Gender = gender And a.Age = age
Dim ads = items.Where(Function(s) s.Advert And s.AdOptions.Any(matching))
Dim sorted = ads _
.OrderBy(Function(a) a.AdOptions.Where(matching).Max(Function(ao) ao.Priority)) _
.ThenByDescending(Function(s) s.Counter)
' if you really wanted a sorted list of AdOptions, you'd write
Dim optionlist = ads.OrderByDescending(Function(s) s.Counter) _
.SelectMany(Function(a) a.AdOptions.Where(matching).OrderBy(Function(ao) ao.Priority))
' to combine the two previous options
Dim adsWithOptions = ads.OrderByDescending(Function(s) s.Counter) _
.Select(Function(a) New With { _
.Ad = a, _
.Options = a.AdOptions.Where(matching) _
.OrderBy(Function(ao) ao.Priority) _
})
End Sub