Toby
Toby

Reputation: 10154

LINQ query ToString returns strange string

I'm trying to get a string from one field of the first record found by a LINQ query as follows:

Dim NameString As String = (From q In DContext.VoxContext.BurnPrograms
                            Where q.Id = acProgramId AndAlso q.Ac = True
                            Select q.ProgramName
                            Take 1).ToString

But when I follow this with:

If NameString = SomeOtherString

NameString is always:

System.Data.Objects.ObjectQuery`1[System.String]

I've tried several different permutations of the query, but can't seem to get it to work. It seems like the query isn't actually being made to run by the ToString. How should this be done correctly?

Thanks

Upvotes: 1

Views: 686

Answers (1)

Nick
Nick

Reputation: 2907

I'm not familiar with VB but this should do it

(From q In DContext.VoxContext.BurnPrograms
                            Where q.Id = acProgramId AndAlso q.Ac = True
                            Select q.ProgramName
                            Take 1).FirstOrDefault().ToString()

or a more elegant solution is to incorporate the lambda expression inside the FirstOrDefault:

var someString = DContext.VoxContext.BurnPrograms.FirstOrDefault(Function(x) x.Id = acProgramId And x.Ac).ProgramName;

Upvotes: 3

Related Questions