1

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?)

4
  • what is ModelSQL.puzzlecontent? This type should be made clear. Commented Aug 16, 2013 at 1:44
  • ModelSQL is the folder name where all my CRUD class file is , puzzlecontent is the name of the CRUD class file that i am using Commented Aug 16, 2013 at 1:45
  • Oh, I overlooked the puzzlecontents. Commented Aug 16, 2013 at 1:46
  • By using new new {g.Hint} you're returning an IEnumerable<> of an anonymous type (what type ever g.Hint is), this doesnt match the signature of the return type of the method (ModelSQL.puzzlecontent). You'll have to select something of type ModelSQL.puzzlecontent and use First/Single(OrDefault) on the result to match the signature of the method. Commented Aug 16, 2013 at 1:52

2 Answers 2

4

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();
Sign up to request clarification or add additional context in comments.

10 Comments

i've add FirstOrDefault() at the end . it says Cannot implicitly convert type 'AnonymousType#1' to 'iStellar.ModelSQL.puzzlecontent'
i am sorry but whats concrete type?
@user2376998 You probably haven't changed g => new {g.Hint} to g => g.Hint
@ClaudioRedi I think the OP wants to get the Hint of the answer, so you should correct the return value to string instead of ModelSQL.puzzlecontent. Otherwise we have to append FirstOrDefault() right after Where without any Select.
@user2376998 but your method returns ModelSQL.puzzlecontent? If your Hint is string, that method should return string.
|
0
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 ^

1 Comment

I'm afraid you can't return var. That won't even compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.