I have a query in a method so I can call i from multiple places like so:
private object GetData(ProfilePropertyDefinition lProfileProperty)
{
return from r in gServiceContext.CreateQuery("opportunity")
join c in gServiceContext.CreateQuery("contact") on ((EntityReference)r["new_contact"]).Id equals c["contactid"] into opp
from o in opp.DefaultIfEmpty()
where ((EntityReference)r["new_channelpartner"]).Id.Equals(lProfileProperty.PropertyValue) && ((OptionSetValue)r["new_leadstatus"]).Equals("100000002")
select new
{
OpportunityId = !r.Contains("opportunityid") ? string.Empty : r["opportunityid"],
CustomerId = !r.Contains("customerid") ? string.Empty : ((EntityReference)r["customerid"]).Name,
Priority = !r.Contains("opportunityratingcode") ? string.Empty : r.FormattedValues["opportunityratingcode"],
ContactName = !r.Contains("new_contact") ? string.Empty : ((EntityReference)r["new_contact"]).Name,
};
}
Then in another method I call the query method like so and try to loop through it:
var exportData = GetData(lProfileProperty);
foreach (var lItem in exportData)
{
}
Then in the same method when I try to loop through the results I keep getting this error on the foreach:
foreach statement cannot operate on variables of type ‘object’ because ‘object’ does not contain a public definition for ‘GetEnumerator’
Any idea what would cause and how to fix it, I’m stumped.
EDIT:
Took Jon’s advice and for the most part it seems to be working. But when I call the method like: GetData<lProfileProperty.PropertyValue>; It says lProfileProperty can’t be found. But it’s there. Any ideas?
EDIT 2: I have everything from Jon’s example in place. I’m getting one error though: On foreach (GridDataItem lItem in exportData) it is saying Error 67 Cannot convert type ‘DotNetNuke.Modules.CPCLeadShare.View.Foo’ to ‘Telerik.Web.UI.GridDataItem’ any ideas on how to fix this? I need to be able to use the DGridDataItem so I can access the “Cells”.
The compiler is telling you what the problem is: you cannot iterate over something that has a static type of
object. Fix the return type of yourGetDatamethod to return something that implementsIEnumerable.Since you are returning a sequence of an anonymous type, you could do this simply by changing the code to
However, you would then be unable to access the properties in the objects except by reflection. To fix that as well, you need to create a new class and return instances of that. For example:
and then