Differences In LINQ Syntax Between .Net And C#?
Jun 29, 2011
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?
View 2 Replies
ADVERTISEMENT
Jul 20, 2011
I wou;d like to know if the synthax used wen writing the codes is still the same in vb05 and vb08, if not wats the difference?
View 1 Replies
Mar 16, 2011
When using VB.NET I can do the following:
Public Function GetUser(ByVal ID as Integer) As User
Dim dc As New YesEntities()
Return (From u in dc.Users Where u.ID = ID).Single()
End Function
And on my .aspx/.vbhtml page I can access the user's department name like this:
Dim DepartmentName as String = new User().GetUser(12).Department.Name
[Code]...
View 1 Replies
Aug 19, 2009
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?
View 6 Replies
Jul 14, 2009
tell me the equivalent syntax for the following code in VB.net?
Static Class TestList
{
static void Main()
{
[code].....
View 3 Replies
May 4, 2011
I ran into this answer which had a triple dot syntax in VB.NET that I have never seen before. The query looks like this
[Code]...
View 3 Replies
May 5, 2009
I'd like to do a subtraction of sets based on criteria. A pseudo-query would look like:
select table1.columnn1
,table1.column2
from table1, table2
[Code]....
I can make it to about here:
dim list = From tbl1 In table1 Where tt.column1 ...
View 1 Replies
May 28, 2009
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]...
View 1 Replies
May 13, 2009
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:
<NewDataSet>
<NewDataTable>
<Field>Accepted ASNs</Field>
<Val>59</Val>[code]....
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.
View 4 Replies
Dec 10, 2010
I have the following XML file
<Test>
<Modules>
<Module Name= "Test1">
<QueueName Name="Test1Active1" Active="True"></QueueName>
<QueueName Name="Test1Active2" Active="True"></QueueName>
[Code]...
View 1 Replies
Aug 6, 2010
I'm trying to grasp the linq to xml 'inline query syntax' features of VB.Net First I tried with this simple xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Child Name="somename">
[code]....
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)
View 1 Replies
Jan 24, 2012
I am building an xml file with the help of Linq queries and I'm not sure why the code below doesn't work.[code]
View 1 Replies
Dec 3, 2009
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.
View 1 Replies
Feb 25, 2011
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.
View 1 Replies
May 25, 2011
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
[Code]...
View 2 Replies
Sep 13, 2011
What is the proper syntax for this:
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]'.
View 5 Replies
Jul 14, 2009
I've been trying to write this snippet of code in VB, can anyone see what is wrong with it? I am recieving an error on line 5.
Dim a1 = From rows In db.tPDMLinkUsages _
Where (rows.ACCESSDATE >= DateTime.Today.AddDays(-90)) _
Select New With _
{ _
[CODE]...
View 5 Replies
Jun 29, 2010
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?
View 2 Replies
Oct 12, 2011
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
View 1 Replies
Dec 16, 2009
Can any one translate the following syntax to vb.net.
m_TextBox.Loaded += TextBoxLoaded
m_TextBox.Loaded -= TextBoxLoaded;
private void TextBoxLoaded(object sender, RoutedEventArgs e)[code].....
View 4 Replies
Aug 22, 2010
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?
View 2 Replies
Aug 10, 2010
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 ?)
View 1 Replies
Aug 14, 2009
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.
View 3 Replies
Jan 14, 2010
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".
View 2 Replies
Dec 13, 2011
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.
View 3 Replies
Dec 18, 2011
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)?
View 1 Replies
Aug 26, 2011
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?
View 2 Replies
Jan 17, 2011
what can C# do that VB.Net can't ? VB.net vs C#.net
main differences between visual basic and C# as well as the pros and cons between the two?
View 1 Replies
Jul 18, 2011
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..
View 2 Replies
Mar 9, 2012
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#?
View 5 Replies