Accessing anonymous types returned via a dynamic link query - linq

I have been trying to access the data returned from a dynamic linq query as an anonymous type. The advice I have found suggests that I should create a custom type and use it in the Select clause with the new keyword. I was directed to the followed Question for a code example:
System.LINQ.Dynamic: Select(" new (...)") into a List<T> (or any other enumerable collection of <T>)
This was indeed an excellent example which I incorporated into my code (which is VB so I had to do some translation).
My code compiles fine but When I run it , I get the error following error:
"Value cannot be null. Parameter name: member" at the following line from the example:
bindings[i] = Expression.Bind(type.GetProperty(properties[i].Name), expressions[i]);
This seems to be linked to expressions(i), which correctly contains two items as I am returning two fields from the database table. properties(i) holds the name of those two fields correctly. Any ideas as to what the value for member is supposed to be and where it should be found? Is it something from the database?
The where clause of this query works and when I run it as an anonymous type it brings back records (or rather two fields from records). The returned fields contain data not nulls.
Here is my VB version of the code from the example provided in the earlier Question. I have bolded or ** the line where the error occurs. Any ideas as to what is causing this?
Much appreciated.
Function ParseNew() As Expression
NextToken()
ValidateToken(TokenId.OpenParen, Res.OpenParenExpected)
NextToken()
Dim properties As New List(Of DynamicProperty)()
Dim expressions As New List(Of Expression)()
Do
Dim exprPos = tokenVal.pos
Dim expr = ParseExpression()
Dim propName As String
If TokenIdentifierIs("as") Then
NextToken()
propName = GetIdentifier()
NextToken()
Else
Dim [me] As MemberExpression = TryCast(expr, MemberExpression)
If [me] Is Nothing Then Throw ParseError(exprPos, Res.MissingAsClause)
propName = [me].Member.Name
End If
expressions.Add(expr)
properties.Add(New DynamicProperty(propName, expr.Type))
If tokenVal.id <> TokenId.Comma Then Exit Do
NextToken()
Loop
ValidateToken(TokenId.CloseParen, Res.CloseParenOrCommaExpected)
NextToken()
'CODE added to support strongly-typed returns
Dim type As Type = If(newResultType, DynamicExpression.CreateClass(properties))
Dim bindings(properties.Count - 1) As MemberBinding
For i As Integer = 0 To bindings.Length - 1
**bindings(i) = Expression.Bind(type.GetProperty(properties(i).Name), expressions(i))**
Next
Return Expression.MemberInit(Expression.[New](type), bindings)
End Function

Related

LINQ query through Azure blobs of IEnumerable(Of IListBlobItem)

I am trying to find specific Uri.AbsolutePath for the Block blob by its name. Azure Storage container contains only Block blobs. List of blobs returned from storage is IEnumerable(Of IListBlobItem).
I use FirstOrDefault to find specific blob by its name. Compiler says there is no Name property for CloudBlockBlob. This is probably related to single blob item type. Even if I use CloudBlockBlob within FirstOrDefault it is still IListBlobItem, thus Name property is missing. How to tackle this in an efficient way?
Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse("Storage connection string")
Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
Dim BlobList As IEnumerable(Of IListBlobItem) = blobClient.GetContainerReference("ContainerName").ListBlobs
Path= If(BlobList.FirstOrDefault(Function(CloudBlockBlob) CloudBlockBlob.Name = "ABC.pdf")?.Uri.AbsolutePath, "")
Sure, FirstOrDefault will result in an IListBlobItem according to intellisense but it has of course an actual implementation. Any debugger will tell you what actual type is returned.
In your case you are only interested in the results of ListBlobs that are actual of type CloudBlockBlob. To do that you can use the OfType method:
Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse("Storage connection string")
Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
Dim BlobList As IEnumerable(Of CloudBlockBlob) = blobClient.GetContainerReference("ContainerName").ListBlobs.OfType(Of CloudBlockBlob)
Path = If(BlobList.FirstOrDefault(Function(CloudBlockBlob) CloudBlockBlob.Name = "ABC.pdf")?.Uri.AbsolutePath, "")
You can probably improve you search by filtering items out on the server side using the prefix option of ListBlobs
Dim BlobList As IEnumerable(Of CloudBlockBlob) = blobClient.GetContainerReference("ContainerName").ListBlobs(prefix := "ABC.pdf").OfType(Of CloudBlockBlob)
This line will only list blobs of which the name starts with / equals to "ABC.pdf".
Disclaimer: I am not a VB.Net developer so there might me some small mistakes and some room for readability improvements.

How to browse a query result?

I have a query:
sqlPU = "SELECT num_uti FROM myTable WHERE C_IDENT_A = '5'
but I didn't know to browse my result.
I try this but it doesn't work
req.Open sqlPU, oConn
If Not req.EOF Then
Set resultSet = oConn.Execute(sqlPU)
For Each result In resultSet
WScript.Echo resultSet.Fields(num_uti).Value
Next
End if
Try restructuring the code like this;
Dim oConn, req, sqlPU
'Create connection
Set oConn = CreateObject("ADODB.Connection")
Call oConn.Open("yourconnectionstring")
'Set query
sqlPU = "SELECT num_uti FROM myTable WHERE C_IDENT_A = '5'"
'Open recordset
Set req = CreateObject("ADODB.Recordset")
Call req.Open(sqlPU, oConn)
'Loop the return data
Do While Not req.EOF
'Output field called num_uti.
WScript.Echo req.Fields("num_uti").Value
'Move to next record
Call req.MoveNext()
Loop
'Clean-up
Call req.Close()
Set req = Nothing
Call oConn.Close()
Set oConn = Nothing
The code in the question has a few issues,
The sqlPU query string is missing a string termination character (") and will cause a syntax error.
When you call .Open() on a ADODB.Recordset it is populated and can be traversed using a Do loop and .MoveNext() method, the extra .Execute() you do is not required and re-runs the same query again. The .MoveNext() method tells the ADODB.Recordset to move it's pointer 1 record, once it reaches the end the value of .EOF will equal True. Without .MoveNext() the pointer will not move past the end of the file (EOF) and the loop will run indefinitely or until the script falls over, neither of which is good.
Referencing columns using .Fields() collection requires either an index (ordinal numeric value starting from 0) or a string containing the alias of the column (in this case "num_uti"). In the original code the column alias wasn't a string which means VBScript would assume it's a variable called num_uti and because num_uti is uninitialised it would fail.

deleting a record in linq to sql (vb.net) what is wrong with my code?

I am getting the correct Employee Id in the VarEmpID variable. When I click on delete
It is giving me
Unable to cast object of type 'System.Data.Linq.DataQuery`1[my name space]' to type 'namespace'.
enter code here
Protected Sub radGrid1_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles radGrid1.DeleteCommand
Dim VarEmpId As String = (CType(e.Item, GridDataItem)).OwnerTableView.DataKeyValues(e.Item.ItemIndex)("EmpId").ToString()
Using dc1 As New EmployeesDataClassesDataContext()
Dim EmployeeEntry = (From p In dc1.Employees
Where (p.EmpId = VarEmpId)
Select p)
dc1.Employees.DeleteOnSubmit(EmployeeEntry)
dc1.SubmitChanges()
Dim queryResults = (From queryItem In EmployeeEntry Select queryItem).ToList()
If queryResults.Any Then
radGrid1.DataSource = queryResults
radGrid1.DataBind()
End If
End Using
End Sub
dc1.Employees.DeleteOnSubmit(EmployeeEntry)
That method expects an Employee instance. Instead, you passed in an employee query.
Dim EmployeeEntry = ( query )
This is a query, not an entry. Consider calling Enumerable.First to get the first result of the query, and then deleting that.
Modified added Dim EmployeeEntry = (From p In dc1.Employees Where (p.EmpId = VarEmpId) Select p).singleorDefault() After that commented out the queryresults part and binded data again it solved my problem. – SmilingLily

Linq Compiled Queries and int[] as parameter

I'm using the following LINQ to SQL compiled query.
private static Func<MyDataContext, int[], int> MainSearchQuery =
CompiledQuery.Compile((MyDataContext db, int[] online ) =>
(from u in db.Users where online.Contains(u.username)
select u));
I know it is not possible to use sequence input paramter for a compiled query and im getting “Parameters cannot be sequences” error when running it.
On another post here related , I saw that there is some solution but I couldn't understand it.
Does anyone know to use complied query with array as input paramter?
Please post example if you do.
Like the post that you referenced, it's not really possible out of the box. The post also references creating your own query provider, but it's a bit of overhead and complexity that you probably don't need.
You have a few options here:
Don't use a compiled query. Rather, have a method which will create a where clause from each item in the array resulting in something like this (psuedo-code):
where
online[0] == u.username ||
online[1] == u.username ||
...
online[n] == u.username
Note that you would have to use expression here to create each OR clause.
If you are using SQL Server 2008, create a scalar valued function which will take a table-valued parameter and a value to compare againt. It will return a bit (to indicate if the item is in the values in the table). Then expose that function through LINQ-to-SQL on your data context. From there, you should be able to create a CompiledQuery for that. Note that in this case, you should take an IEnumerable<string> (assuming username is of type string) instead of an array, just because you might have more than one way of representing a sequence of strings, and to SQL server for this operation, it won't matter what the order is.
One solution that I have found myself doing (for MS SQL 2005/2008). And I'm not sure if it is appropriate in all scenarios is to just write dynamic sql and execute it against the datacontext using the ExecuteQuery method.
For example, if I have an unbounded list that I am trying to pass to a query to do a contains...
' Mock a list of values
Dim ids as New List(of Integer)
ids.Add(1)
ids.Add(2)
' ....
ids.Add(1234)
Dim indivs = (From c In context.Individuals _
Where ids.Contains(c.Id) _
Select c).ToList
I would modify this query to create a SQL string to execute against the database directly like so...
Dim str As New Text.StringBuilder("")
Dim declareStmt as string = "declare #ids table (indivId int) " & vbcrlf)
For i As Integer = 0 To ids.Count - 1
str.Append("select " & ids(i).ToString() & " & vbcrlf)
If i < ids.Count Then
str.Append("union " & vbcrlf)
End If
Next
Dim selStatement As String = "select * From " & context.Mapping.GetTable(GetType(Individuals)).TableName & _
" indiv " & vbcrlf & _
" inner join #ids ids on indiv.id = ids.id"
Dim query = declareStmt & str.ToString & selStatement
Dim result = context.ExecuteQuery(of Individual)(query).ToList
So barring any syntax errors or bugs that I coded (the above is more or less psuedo code and not tested), the above will generate a table variable in SQL and execute an inner join against the desired table (Individuals in this example) and avoid the use of a "IN" statement.
Hope that helps someone out!

rss feed by linq

I am trying to extract the rss feed using linq. Thought it would be simple, but its is not returning any nodes. probably i have to go the channel/item node, but don't know how.
Dim rssUrl As String = "http://webclip.in/rss.aspx?u=mostliked"
Dim rssDoc As XDocument = XDocument.Load(rssUrl)
Dim rssResultSet = From node In rssDoc.Descendants("item") _
Select New With { _
.title = node.Element("title").Value, _
.link = node.Element("link").Value, _
.description = node.Element("description").Value, _
.pubDate = Date.Parse(node.Element("pubdate").Value) _
}
DataGridView1.DataSource = rssResultSet
Two issues here... First, you should correct this line:
.pubDate = Date.Parse(node.Element("pubDate").Value)
The pubDate is a case-sensitive node in XML. Secondly, your dataSource will never work because LINQ is lazy computation. You have to use ToList() or a similar method that enumerate your collection. If you debug within Visual Studio 2010, you'll see that rssResultSet does not have a value because it is only enumerated when your code calls for it. Replace with this:
DataGridView1.DataSource = rssResultSet.ToList()
My last piece of advice is to set your DataGrid to AutoGenerate it's columns.
the casing on pubdate is wrong. It should be "pubDate". otherwise, works fine.

Resources