user2376998
user2376998

Reputation: 1071

Lambda expressions from sql

i am trying to do this in lambda :

Select Hint from [tablename] where Answer = 'answer';

this is what i have tried so far :

    public ModelSQL.puzzlecontent GetAcrossClue(string answer)
    {

        return context.puzzlecontents.Where(c => c.Answer.Equals(answer)).Select( g => new {g.Hint});
    }

Error says :

Cannot implicitly convert type 'System.Linq.IQueryable' to 'iStellar.ModelSQL.puzzlecontent'. An explicit conversion exists (are you missing a cast?)

Upvotes: 1

Views: 162

Answers (2)

coeyb
coeyb

Reputation: 13

public var GetAcrossClue(string answer)
{

    return context.puzzlecontents.Where(c => c.Answer.Equals(answer)).Select( g => new {g.Hint});
}

ignore this part, thanks claudio. It's late what can I say?

or

public ModelSQL.puzzlecontent GetAcrossClue(string answer)
{

    return context.puzzlecontents.Where(c => c.Answer.Equals(answer)).Select( g => new puzzlecontent{property1 = value,property2 = etc});
} 

this part'll work though ^

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

Your problem is that Select returns a colection and your method returns a single instance.

Assuming that g.Hint is a ModelSQL.puzzlecontent instance, you should add FirstOrDefault at the end to retrieve a single item.

Something that I missed is that you're trying to return an anonymous type trough new { g.Hint } , that's not valid. You need to return a concrete type.

Again, Assuming that g.Hint is a ModelSQL.puzzlecontent instance, you should have

return context.puzzlecontents
    .Where(c => c.Answer.Equals(answer))
    .Select(g => g.Hint)
    .FirstOrDefault();

Upvotes: 4

Related Questions