Need help with a LINQ Query using the Dynamic LINQ Library - vb.net

Forgive my ignorance on this.
I have this LINQ Query:
Dim ngBikersDataContext As New CarBikeWalkDataContext
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 _
}).ToList()
As you can see it is a LIST(OF ). Here is the definition of the bikersList:
Dim bikersList As List(Of Bikers) = TryCast(HttpContext.Current.Session("Bikers"), List(Of Bikers))
I need to be able to sort, so was going to use the Dynamic LINQ Library. So I added it to my project Imported System.Linq.Dynamic and tried to use this code:
bikersList = (ngBikersDataContext.Reg_Bikers _
.OrderBy(SortExpression) _
.Select New Bikers() With { _
.BikerID = c.BikerID, _
.F_Name = c.F_Name, _
.M_Name = c.M_Name, _
.L_Name = c.L_Name _
}).ToList()
But now I am getting the blue scwiggly line under:
ngBikersDataContext.Reg_Bikers _
.OrderBy(SortExpression) _
.Select
with the error "Overload resolution failed because no accesible 'Select' accepts this number of arguments."
Over the "NEW" I get an error " ')'expected."
Would someone please tell me what I am doing wrong.
Thank You

You're mixing up 'extension method' syntax with 'query syntax'.
bikersList = (ngBikersDataContext.Reg_Bikers _
.OrderBy(SortExpression) _
.Select(Function(c) New Bikers() With { _
.BikerID = c.BikerID, _
.F_Name = c.F_Name, _
.M_Name = c.M_Name, _
.L_Name = c.L_Name _
})).ToList()
Or
bikersList = (From c in ngBikersDataContext.Reg_Bikers.OrderBy(SortExpression) _
Select b = New Bikers() With { _
.BikerID = c.BikerID, _
.F_Name = c.F_Name, _
.M_Name = c.M_Name, _
.L_Name = c.L_Name _
}).ToList()

Related

local variable 'request' cannot be referred to before it is declared

Dim list = New List(Of ECozum.HPM.Helper.Models.CustomData)
list.Add(cdata)
Dim request = New HttpPostRequestMessage() With { _
.User = New User() With { _
.Verify = 1, _
.VerifyFailAct = 1, _
.Id = 2 _
}, _
.Order = New Order() With { _
.DateTime = DateTime.UtcNow.ToString("u"), _
.Reference = Guid.NewGuid().ToString() _
}, _
.Payment = New Payment() With { _
.CData = list, _
.Method = New List(Of Integer)() From { _
-1 _
}, _
.Amount = tutar, _
.AmntEdit = 1, _
.SuccessUrl = "http://localhost:50/sonuc.aspx?s=basarili", _
.FailUrl = "http://localhost:50/sonuc.aspx?s=basarisiz", _
.ReturnUrl = "http://localhost:50/sonuc.aspx?s=return" _
}, _
.HashMethod = CInt(Hash.HashType.HMACSHA256) _
}
In this page I get the error:
"local variable 'request' cannot be referred to before it is declared"
at line: tutar = Request.QueryString("tutar")
Dim request = New HttpPostRequestMessage() change this line. Use any random name like 'requestobject' or 'rnd123'. Since you have it declared as a variable, it is conflicting with default request object.

How could I make a mDNS Query with Pcap.net?

Problem: I'm looking to make a mDNS packet, while having searching stackflow for options. I tried bonjour and some wrappers but had very limited success, especially when I requested a second time and get socket binding complaints (Which, of course, may have been my code not them).
Since VB.net didn't have a really editable dnsquery that I know of, I'm using the DNS layer in the build DNS packet in pcapdotnet and just kind of making the packet myself layer by layer. I'm thinking it's a good alternative, but I'm kind of lost on how I would do it.
Here's the question we want:
q_name = new QuestionName("_axis-video._tcp.local"),
q_type = QueryConstants.Question.QuestionType.PTR,
q_class = QueryConstants.Question.QuestionClass.IN
Here's my edited BuildDNSPacket function from their standard:
Private Shared Function BuildDnsPacket(destmac As String, domainName As String) As Packet
'get source MAC address of PC
Dim nic = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
Dim source As String = nic(0).GetPhysicalAddress().ToString
Dim sourcearray As Byte() = System.Text.Encoding.ASCII.GetBytes(source)
'format
Dim sourceMacStr As String = ""
For i As Integer = 0 To sourcearray.Count - 1 Step 2
sourceMacStr += Chr(sourcearray(i)) & Chr(sourcearray(i + 1)) & ":"
Next
' Will be filled automatically.
Dim ethernetLayer As New EthernetLayer() With { _
.Source = New MacAddress(sourceMacStr.Substring(0, 17)), _
.Destination = New MacAddress(destmac), _
.EtherType = EthernetType.None _
}
' Will be filled automatically.
Dim ipV4Layer As New IpV4Layer() With { _
.Source = New IpV4Address("1.2.3.4"), _
.CurrentDestination = New IpV4Address(destmac), _
.Fragmentation = IpV4Fragmentation.None, _
.HeaderChecksum = Nothing, _
.Identification = 123, _
.Options = IpV4Options.None, _
.Protocol = Nothing, _
.Ttl = 100, _
.TypeOfService = 0 _
}
' Will be filled automatically.
Dim udpLayer As New UdpLayer() With { _
.SourcePort = 5353, _
.DestinationPort = 5353, _
.Checksum = Nothing, _
.CalculateChecksumValue = False _
}
Dim dnsLayer As New DnsLayer() With { _
.Id = 0, _
.IsResponse = False, _
.OpCode = DnsOpCode.Query, _
.IsAuthoritativeAnswer = False, _
.IsTruncated = False, _
.IsRecursionDesired = False, _
.IsRecursionAvailable = False, _
.FutureUse = False, _
.IsAuthenticData = False, _
.IsCheckingDisabled = False, _
.ResponseCode = DnsResponseCode.NoError, _
.Queries = {New DnsQueryResourceRecord(New DnsDomainName(domainName), DnsType.Ptr, DnsClass.Any)}, _
.Answers = Nothing, _
.Authorities = Nothing, _
.Additionals = Nothing, _
.DomainNameCompressionMode = DnsDomainNameCompressionMode.All _
}
Dim builder As New PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, dnsLayer)
Return builder.Build(DateTime.Now)
End Function
The main differences is my changing the DnsType to PTR and the port to 5353.
Question: What else should I add or change to make it mDNS? What could I put into the domainName? Should I vary the dnsclass?
All or any suggestions are definitely welcomed.
I am answering my question in case others who need to do mDNS in vb.net needs this:
Solution: I didn't need to add anything to the DNS layer to make this work. I changed the DNS layer to below:
Dim dnsLayer As New DnsLayer() With { _
.Id = 0, _
.IsResponse = False, _
.OpCode = DnsOpCode.Query, _
.IsAuthoritativeAnswer = False, _
.IsTruncated = False, _
.IsRecursionDesired = False, _
.IsRecursionAvailable = False, _
.FutureUse = False, _
.IsAuthenticData = False, _
.IsCheckingDisabled = False, _
.ResponseCode = DnsResponseCode.NoError, _
.Queries = {New DnsQueryResourceRecord(New DnsDomainName(domainName), DnsType.Ptr, DnsClass.Any)}, _
.Answers = Nothing, _
.Authorities = Nothing, _
.Additionals = Nothing, _
.DomainNameCompressionMode = DnsDomainNameCompressionMode.All _
}
I made the output address of the Ipv4 layer to be the multicast address of "224.0.0.251", changed my ports to 5353, and used the domain name of the question I listed above.
Here's a wireshark to show the responses:

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

How to use the vb equivalent of ++ for an index property in a Linq query projecting a new type

I'd normally do this in C# but since I've got to get this code in this particular assembly which is a vb.net one, I'm stuck.
Here's my linq query:
Dim i As Integer = 0
Dim oldAndCurrentIntersectionOnNames = From currentApplicant In currentApplicants _
Group Join oldApplicant In oldApplicants _
On _
New With {Key .FirstName = currentApplicant.FirstName, _
Key .LastName = currentApplicant.LastName} _
Equals _
New With {Key .FirstName = oldApplicant.FirstName, _
Key .LastName = oldApplicant.LastName} Into applicants = Group _
From applicant In applicants.DefaultIfEmpty(New ApplicantNameDetails()) _
Select New ApplicantNameDetails() With _
{ _
.Index = i+=1, _
.FirstName = CStr(IIf(Not currentApplicant.FirstName Is Nothing, currentApplicant.FirstName, Nothing)), _
.OldFirstName = CStr(IIf(Not applicant.FirstName Is Nothing, applicant.FirstName, Nothing)), _
.LastName = CStr(IIf(Not currentApplicant.LastName Is Nothing, currentApplicant.LastName, Nothing)), _
.OldLastName = CStr(IIf(Not applicant.LastName Is Nothing, applicant.LastName, Nothing)) _
}
You'll see the .Index = i+=1
This was my attempt to do what I'd quite happily do in C# (i.e. Index = i++) in VB. Unfortunately the VB compiler doesn't like that.
Has anybody got any suggestions as to how I'd do this in VB.
Thanks in advance
Essentially, you can’t. If you want the Linq query to get consecutive values, use a special (so-called “generator”) class that has an IncrementAndGet (or simply Next) method for your integer.
class IntegerGenerator
private state as integer = 0
public function Next() as integer
dim oldState = state
state += 1
return oldState
end function
end class
There is an overload of the Select method that lets you use the index of the item on the result collection. http://msdn.microsoft.com/en-us/library/bb534869.aspx
You could split your query in two parts to use it (untested)
Dim q = From currentApplicant In currentApplicants _
Group Join oldApplicant In oldApplicants On _
New With {Key.FirstName = currentApplicant.FirstName, _
Key.LastName = currentApplicant.LastName} _
Equals _
New With {Key.FirstName = oldApplicant.FirstName, _
Key.LastName = oldApplicant.LastName} Into applicants = Group _
From applicant In applicants.DefaultIfEmpty(New ApplicantNameDetails())
Dim oldAndCurrentIntersectionOnNames = _
q.Select(Function(x, i) New ApplicantNameDetails() With _
{ _
.Index = i, _
.FirstName = CStr(IIf(Not x.currentApplicant.FirstName Is Nothing, x.currentApplicant.FirstName, Nothing)), _
.OldFirstName = CStr(IIf(Not x.applicant.FirstName Is Nothing, x.applicant.FirstName, Nothing)), _
.LastName = CStr(IIf(Not x.currentApplicant.LastName Is Nothing, x.currentApplicant.LastName, Nothing)), _
.OldLastName = CStr(IIf(Not x.applicant.LastName Is Nothing, x.applicant.LastName, Nothing)) _
})

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