user3046164
user3046164

Reputation: 159

Linq .Count() does not exist in VB.NET but exists in C#

This linq question works excellent in c# and the field AntalVersioner = d.Count() works perfekt.

List<DokumentModel> result = (from d in response.Values
                                            from c in d
                                            //let dk = response.
                                            select new DokumentMedDatumDTOModel()
                                            {
                                                DokumentId = c.DokumentId,
                                                Forfattare = c.Forfattare,
                                                Kommentar = c.Kommentar,
                                                Nyckelord = c.Nyckelord,
                                                AntalVersioner = d.Count(),
                                                VersionNr = c.VersionNr,
                                                Titel = c.Titel,
                                                SkapadAv = c.SkapadAv,
                                                OriginalFilNamn = c.OriginalFilnamn,
                                                Versions = (from p in d
                                                            select new Version()
                                                            {
                                                                FilUppladdatDatum = p.FilUppladdadDatum,
                                                                OriginalFilNamn = p.OriginalFilnamn,
                                                                SkapadAv = p.SkapadAv,
                                                                Titel = p.Titel,
                                                                VersionNr = p.VersionNr
                                                            }).ToList(),

                                            }).ToList();

When Trying to the same thing in VB.NET the d.Count() does not exist

result = (From dokument In response.Values
            From d In dokument
            Select New DokumentMedDatumDTOModel With {.DokumentId = d.DokumentId, .Forfattare = d.Forfattare, .Kommentar = d.Kommentar, _
                                            .Nyckelord = d.Nyckelord, .VersionNr = d.VersionNr, .Titel = d.Titel, .SkapadAv = d.SkapadAv, _
                                            .OriginalFilNamn = d.OriginalFilnamn, _
                                            .Versions = (From t In dokument
                                                        Select New DokumentVersion With {.Forfattare = t.Forfattare, _
                                                                                        .DokumentId = t.DokumentId, _
                                                                                        .FilUppladdatDatum = t.FilUppladdadDatum, _
                                                                                        .OriginalFilNamn = t.OriginalFilnamn, _
                                                                                        .SkapadAv = t.SkapadAv, _
                                                                                        .Titel = t.Titel, _
                                                                                        .Kommentar = t.Kommentar, _
                                                                                        .Nyckelord = t.Nyckelord, _
                                                                                        .VersionNr = t.VersionNr}).ToList()
                                        }).ToList()

Upvotes: 0

Views: 344

Answers (2)

JWP
JWP

Reputation: 6963

Seems hard for me to believe that LINQ which is a framework, works differently for Vb than C#. But maybe it does, however the count function is so fundamental that I have to believe something else is amiss. Just because intellisense doesn't show doesn't mean it's not there. It just means intellisense isn't showing. When it comes to writing LINQ queries, intellisense will quit responding as soon as something in the query isn't right.

Start simple like this.

var stuff = db.Customers.ToList();
var count = stuff.Count(); 

Upvotes: 0

David
David

Reputation: 218960

Your two code samples seem to be doing very different things on different types.

For example, in C# you create a model:

select new DokumentMedDatumDTOModel()

But in VB you create a different model:

Select New DokumentModel

So I suspect a few things are going to be different between these, regardless of the language being used.

More specific to the question being asked, if d.Count() "works" in C#, let's take a look at what d is:

from d in response.Values
from c in d

d is an instance from the response.Values collection. But what is it in VB?:

From dokument In response.Values
From d In dokument

d is an instance of the collection of the instances from response.Values. Completely different object. The "equivalent" of d.Count() from this VB code would be:

dokument.Count();

The compiler doesn't check the name of the variable for anything, it just checks the type of the instance. Your d variables in the two code samples are entirely different types/instances.

Upvotes: 3

Related Questions