ForEach over Object错误

时间:2012-01-10 15:49:56

标签: c# .net exception-handling foreach ienumerable

我在一个方法中有一个查询,所以我可以从多个地方调用i,如下所示:

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,
           };
}

然后在另一个方法中,我调用查询方法,并尝试循环遍历它:

var exportData = GetData(lProfileProperty);
foreach (var lItem in exportData)
{
}

然后在我尝试循环结果的同一方法中,我一直在foreach上得到这个错误:

  

foreach语句不能对'object'类型的变量进行操作,因为'object'不包含'GetEnumerator'的公共定义

任何想法会导致什么以及如何解决它,我很难过。

编辑:

接受Jon的建议,并且在大多数情况下它似乎正在起作用。但是当我调用方法时:GetData<lProfileProperty.PropertyValue>;它说lProfileProperty无法找到。但它就在那里。有什么想法吗?

编辑2:我有Jon的例子。我收到一个错误:在foreach (GridDataItem lItem in exportData)上它说错误67无法将类型'DotNetNuke.Modules.CPCLeadShare.View.Foo'转换为'Telerik.Web.UI.GridDataItem'关于如何解决此问题的任何想法?我需要能够使用DGridDataItem,以便我可以访问“Cells”。

7 个答案:

答案 0 :(得分:5)

编译器告诉你问题是什么:你不能迭代具有静态类型object的东西。修复GetData方法的返回类型,以返回实现IEnumerable的内容。

由于您返回的是匿名类型的序列,因此只需将代码更改为

即可
private IEnumerable GetData(ProfilePropertyDefinition lProfileProperty) 

但是,您将无法访问除反射之外的对象中的属性。要解决这个问题,您需要创建一个新类并返回它的实例。例如:

class Foo {
    public string OpportunityId { get; set; }
    public string CustomerId { get; set; }
    public string Priority { get; set; }
    public string ContactName { get; set; }
}

然后

private IEnumerable<Foo> GetData(ProfilePropertyDefinition lProfileProperty) {
   // ...
   select new Foo
       { 
           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, 
       }; 
    // ...
}

答案 1 :(得分:3)

您的方法返回object。 object没有实现IEnumerable<T>。您也无法转换为IEnumerable<T>,因为您的方法使用匿名类型。

您唯一的选择是为方法的返回类型创建一个具体的类。

答案 2 :(得分:2)

您需要返回一种实现IEnumerable的GetData()类型。我建议将方法签名中的对象链接到IEnumerable。

IEnumerable包含一个方法GetEnumerator(),它允许foreach循环工作。

答案 3 :(得分:1)

您的方法正在返回object,而不是任何类型的集合或列表

答案 4 :(得分:1)

您收到错误是因为从GetData(...)收到的变量属于object类型。你不能枚举object,因为它就是它 - 一个对象而不是一个集合。要枚举方法调用的结果,您需要将对象强制转换为 集合的类型,因此具有 GetEnumerator(...)方法。

答案 5 :(得分:0)

您需要返回IEnumerable<Some>才能循环播放它。目前您只返回object

但是你不能从方法返回一个匿名类型,所以你需要声明一个特定的类型:

class Foo
{
    ...
}

private IEnumereable<Foo> GetDate()
{
    return ...
           select new Foo
           {
               ...
           };
};

答案 6 :(得分:0)

尝试这样的事情:

private object GetData(ProfilePropertyDefinition lProfileProperty)
{
    var result = 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,
                 });

                 return result.ToList();
    }

foreach (var lItem in List<object>GetData(lProfileProperty))
{
}

并添加一些类

select new  [YouClassView]  
                 {
                     OpportunityId = !r.Contains("opportunityid") ? string.Empty : r["opportunityid"],
                     Custo