views:

25

answers:

1

I pass two parameters to my repository to return one record

I am strugling to wite the code to return one record.

Here is my repository-

  public Classifieds_Ads GetUserClassifiedDetailsToModify(int classifiedid, Guid UserGuid)
        {
            return context.Classifieds_Ads.Where(c => c.User.Id == UserGuid && c => c.CatID == classifiedid);        
        }

I think you guys can see what I am trying to do but I just write the code. Can I have some help please!

It annoying because I know I will be licking mt self when i see the soloution.

I am also using a stroingly typed views.

Thanks,

A: 

Try this:

public Classifieds_Ads GetUserClassifiedDetailsToModify(int classifiedid, Guid UserGuid)
{
    return context.Classifieds_Ads.Where(c => c.User.Id == UserGuid && c.CatID == classifiedid).First();        
}

If your data is returning more than one row you could ask it to return the first row by using the First extension method:

Where(c => c.User.Id == UserGuid && c.CatID == classifiedid).First()
adriaanp
In case you don't want an exception if there are no matches, you could also use `.FirstOrDefault()` which will return `null` for value types if no matches are found.
Tomas Lycken
Nope I think I have tried this already, just tried it again and it threw me an error. Thank you anywaym
Csharper
opps hang on i may have overlooked something.
Csharper
Well it kind of works... it just doesn't return anything. The variables have all been given values this was confirmed when i hovered my mouse over them. and I have checked the database and they are definatly there I just don'e know what is going on.
Csharper