After I have programmed several projects in VB.Net I to my surprise discovered that there are some more than subtle differences between C# and VB.NET LINQ usage.For example, if we want to group elements by multiple properties (columns) we need to create a new anonymous type explicitly:
var procs = from c in Process.GetProcesses()
group c by new {c.BasePriority, c.Id} into d
select d;
whereas in VB.NET more straightforward syntax will already do:
Dim b = From c In Process.GetProcesses()
Group c By c.BasePriority, c.Id Into Group
Select Group
So, one does not need to create a type with "new" here. What are the other differences? good comparison between the LINQ syntax in C# and VB.NET?
I have a LINQ query to retrieve the maximum value of an integer column. The column is defined as NOT NULL in the database. However, when using the MAX aggregate function in SQL you will get a NULL result if no rows are returned by the query.Here is a sample LINQ query I am using against the Northwind database to demonstrate what I am doing.
var maxValue = (from p in nw.Products where p.ProductID < 0 select p.ProductID as int?).Max();
C# correctly parses this query and maxValue has a type of int?. Furthermore, the SQL that is generated is perfect:
SELECT MAX([t0].[ProductID]) AS [value] FROM [Products] AS [t0] WHERE [t0].[ProductID] < @p0
The question is, how do I code this using VB.NET and get identical results? If I do a straight translation:
dim maxValue = (from p in Products where p.ProductID < 0 select TryCast(p.ProductID, integer?)).Max()
I get a compile error. TryCast will only work with reference types, not value types. TryCast & "as" are slightly different in this respect. C# does a little extra work with boxing to handle value types. So, my next solution is to use CType instead of TryCast:
dim maxValue = (from p in Products where p.ProductID > 0 select CType(p.ProductID, integer?)).Max()
This works, but it generates the following SQL:
SELECT MAX([t1].[value]) AS [value] FROM ( SELECT [t0].[ProductID] AS [value], [t0].[ProductID]
[code]....
While this is correct, it is not very clean. Granted, in this particular case SQL Server would probably optimize the query it to be the same as the C# version, I can envisage situations where this might not be the case. Interestingly, in the C# version, if I use a normal cast (i.e. (int?)p.ProductID) instead of using the "as" operator I get the same SQL as the VB version. if there is a way to generate the optimal SQL in VB for this type of query?
What I really want is to select these two tables in to an anon type like in Scott Gu's blog: here However, I would settle for this created type "ActiveLots" I am joining two tables together and want to be able to reference columns from each in my result set. [code]...
I'm a bit of a LINQ newbie and I was having some trouble with the following. I'm trying to perform a query using LINQ on an XML file and store the results in a list of DataClass objects that match the XML.I've got an XML file that is defined like this:
I have created a Data class to support this XML format. What I would like to do is create a LINQ Query to grab these 3 records and store them into a List of my DataClass. In order to support multiple Order elements, I have my class defined with a generic list of "Order" structs... It looks like this:
Public Class ASNData Private _field As String Private _value As String[code].....
how to grab the 3 order elements and store them into my list of Order structs.
This xml, when loaded in an XDocument, can be loaded and queried as follows:
Dim xdoc = XDocument.Load("sample.xml") Console.WriteLine(xml.Root.<Child>.@Name)
Then I change the <Root> element in the sample xml file to:
<Root xmlns="http://SomeNamespace">
Now I can't seem to use the convenient 'Axis Properties' syntax anymore... I can only get it to work with the explicit XElement syntax:
Dim ns As XNamespace = "http://SomeNamespace" ' works, but I would like to use the same syntax as above... Console.WriteLine(xdoc.Descendants(ns + "Child").First().Attribute("Name").Value)
I am trying to AVERAGE 6 different fields on a DataTable, but without grouping. Just the AVERAGE. I have found a couple of examples in C#, but can't find any examples for how to do this in VB.Here is what I have so far:
Dim query = From dtRow In dtIn.AsEnumerable _ Where dtRow.Field(Of String)("FOLLOWUP") = "Alert" _ Select New With { _ .Brand_Functional_Avg = XXX.Average(Function(f) f("Brand_Functional")), _ .Brand_Personal_Avg = XXX.Average(Function(f) f("Brand_Personal")) _ }
What should I use for XXX? I tried all of the options that I could think of and nothing is compiling.
Trust me, if I could write this in C#, I would, but the project requires VB.
I have a query that I cannot seem to replicate in expression method chain syntax. I have two tables "User" and "UserPayment". User and UserPayment have a one to many relation i.e. One User can have many UserPayments.
I have a LINQ to sql statement that joins 2 tables. I would like to add a order by clause on one of the columns. However the order by clause does not seem to take effect at all. right syntax in VB.net to achieve order by in the following:
Dim query = From dtIt In dbsomecontext.mytable Join dtIl In dbsomecontext.anothertable On dtIt.ItemID Equals dtIl.ItemID Where dtIl.IsAvailable = True Order By dtIt.manufacturer
Dim qry As <??> = From f In dirInfo.GetFiles("*.QBW") Select File = f.FullName, Include = True Dim dt As DataTable = qry.CopyToDataTable()
I tried as "IEnumerable(Of DataRow)" but that didn't work. At runtime it said:
Unable to cast object of type 'WhereSelectArrayIterator2[System.IO.FileInfo,VB$AnonymousType_02[System.String,System.Boolean]]' to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataRow]'.
I'm trying to translate some C# LINQ code into VB.NET and am stuck on how to declare an anonymous type in VB.NET.[code]How do you translate C#'s new { ... } syntax into VB.NET?
I am having an issue trying to figure this out. I am writing a script editor that uses tabs (a tab control) and I want to implement syntax highlighting. My issue is that every sample I can find on syntax highlighting uses
Private Sub RichTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
What are the differences between 32 and 64-bit .NET (4) applications?Often 32-bit applications have problems running on 64-bit machines and conversely. I know I can declare an integer as int32 and int64 (certainly int64 on 32-bit systems make problems). Are there other differences between programming an 32 OR 64-bit or a both 32 AND 64-bit compatible application?
Would like to build an application and was wondering why someone would choose one language over another.Also is one language used more for a particular type of programming then another (For example, is VB used more for business or Windows development then for web development ?)
I am currently learning visual basic 5 on my home computer. I was wondering if someone could tell me what the major differences are between Visual basic 5 and the latest/new editions of VB.
We have legacy character codes that we want to store as numbers in a new system. To increase readibility and general understanding in the code for devs making the migration, I want to do Enums like this..
[Code]...
With this setup, the code will be readable (imagine If Record.Status = Status.Open), and yet the values will be stored in the database as small numbers so it will be efficient. However... I am a VB.NET guy, but everybody wants to code in C#, so I need this sort of structure in C#.After Googling, I discovered the the general .NET equivalent of AscW is Convert.ToInt32("C"). When I try to use that statement in an enum, I get the compiler error "Constant Expression Required".
in order for me to get help on me and my programming partners vb6 rpg, I have to upgrade the project to the vb.net platform, and I am worried that the IDE interface that is used to program the vb side of the rpg is different, so I am wanting to ask any of you if there are any significant changes to the IDE or if it is relatively the same as it was in vb6... I am also hoping that someone here can also help me to see what is wrong with my load/save function... I now thought of something to try to see if that is the issue or not, but the code is still vb6 code, the only difference will be that the program will be upgraded to the .net platform, so if that changes anything in the code, then I am not aware of this as of yet, because I am still downloading the visual studios 2010 express iso right now... so I do not know for sure yet what will be changed.
Does anyone know how to implement in VB an algorithm for final differences that changes the network (the matrix) during the computation (decrease its dimensions)?
Today I was playing around with Entity Framework and I've read that the generated IL for C# was different than VB.NET for the following code:
[code]...
As it seems the VB.NET version of this code will contact the database every time the code is executed while the C# version will retrieve the entities from the cache when the code is executed multiple times.Why would they make both languages behave in such a different manner?It was my misconception that both languages just differed in syntax and had almost exactly the same generated IL.Are there any more examples where both languages generated such different IL?
Well i want to compare 2 strings (version one and version two) and get the differences in a format that i can convert to html on my own, like you can view how a post was edited here on stack*overflow* or like svn tracks differences between revisions....
It must be full managed code library.
Like this JavaScript but i need to do it on the server-side..
I've heard that the c# garbage collector can be 'more aggressive' than it's vb.net counterpart. Is this true? Are there any other differences in how garbage collection is run in vb.net vs. c#?