1

I like to use keyword where, for example.

I simply do it like this

Dim orderFromThisGridTrading = _orders1.Where(Function(x) x.customIdentifier = ASimpleGridTradeSettings.name).ToArray

However, most samples would say I have to use from

Dim customersForRegion = From cust In customers Where cust.Region = region

Which is weird af and don't follow normal vb.net format. That looks a like like SQL languages and not a real programming language.

I wonder if I can always avoid using Form and just use like I am using? Are there any cases where that is not possible?

Is this even a good idea?

user4951
  • 32,206
  • 53
  • 172
  • 282

2 Answers2

4

There is nothing bad in using query syntax in general. Especially in VB.NET it is very powerful and supports a lot of LINQ methods(more than in C#). Many prefer that because it can make your code more readable. But it seems that the opposite is true for you, you don't like it. Then use the method syntax, it supports all methods. I don't like it in VB.NET because of that ugly and verbose Function keyword, especially with GroupBy or Join i prefer query syntax.

I wonder if I can always avoid using From and just use like I am using? Are there any cases where that is not possible?

No, method syntax is always possible. Actually query syntax gets compiled to method syntax.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3

The second example is called "Query Comprehension Syntax". It's optional. A lot of people find it makes their code more readable, but not everyone likes it. Personally, I find it adds another extra layer of indirection for what you need to know to get the computer to do what you want.

In the first example, the "From" is implied by the intial IEnumerable or IQueryable item in the expression.

But I do have one issue:

SQL is definitely a real programming language, and you'll likely get even better results from learning it well vs needing to rely on an ORM to construct queries. Eventually, usually sooner than later, you'll find you want to do something where you need to know advanced SQL anyway. Then you have two problems, because you need to the know the SQL and you need to know how to construct the ORM syntax to produce that SQL.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794