3

I am using Entity Framework. I have the following:

var db = new LikEntities();

GetParamAlerts_Result paramRslt = db.GetParamAlerts();

GetParamAlerts is a stored procedure and it is of type

System.Data.Objects.ObjectResult<GetParamAlerts_Result>

Note the GetParamAlerts returns multiple rows.

When I run the code above I the following error message :

Cannot implicitly convert type:

'System.Data.Objects.ObjectResult' to 'PVT_Alert_Notification.GetParamAlerts_Result'

Not sure how to resolve this.

4
  • Linq to SQL is not the same as LINQ to Entities. Is this Entity Framework or LINQ to SQL? Commented Aug 6, 2013 at 16:11
  • This is Entity Framework Commented Aug 6, 2013 at 16:16
  • @NatePet, are you returning an IList<GetParamAlerts_Result> from this method? Commented Aug 6, 2013 at 16:21
  • @NatePet, it would be also helpful to tell us what the desired result is i.e. do you only want one object? do you want a collection? etc. The code above suggest you want one object, your comments suggest you want a list, so it's confusing. Commented Aug 6, 2013 at 16:30

3 Answers 3

5

This is because Entity Framework cannot guarantee that your stored procedure will always return a single row, so it's put into a ObjectResult which is just an enumerable collection. If you're always expecting a single result, you can use db.GetParamAlerts().Single() to get the result as GetParamAlerts_Result, or use any of the standard enumerable methods like First(), FirstOrDefault(), SingleOrDefault(), etc.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Steven, not that it will return multiple rows. Can you tweak the code above to reflect that. I did a ToList() but got a similar error message.
@NatePet I don't quite understand your question. Doing a ToList() really won't do anything to cast a collection to a single type, resulting in a similar casting error.
@NatePet System.Data.Objects.ObjectResult<GetParamAlerts_Result> is pretty much a list already. No need for ToList() or anything else. Just use GetParamAlerts_Result paramRslt = db.GetParamAlerts().Single(); and it'll work.
Steven, in my case, I am expecting more than 1 result. If I use the Single() it will return just 1 results. How can the code be tweaked to allow for multiple results.
@NatePet Then make it ObjectResult<GetParamAlerts_Result> paramRslt = db.GetParamAlerts(); and then paramsRslt will have the collection of results which you can iterate over, or send onto a view.
|
1

it looks like you should either be using

PVT_Alert_Notification.GetParamAlerts_Result paramRslt = db.GetParamAlerts();

or the method is returning the wrong type? Can you pot the code for the method? or you are getting a collection?

If you have a collection, you could use:

var result = db.GetParamAlerts();
return result.FirstOrDefault();

You will see a similar post here

Comments

0

try incorporating first or default at the end

 GetParamAlerts_Result paramRslt = db.GetParamAlerts().FirstOrDefault();

try using cast

  GetParamAlerts_Result paramRslt = db.GetParamAlerts().Cast<GetParamAlerts_Result>().ToList(); 

1 Comment

Thanks but I am getting a list. Can you kindly tweak the code to reflect that. I did a .ToList() but that did not work.

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.