LINQ中无法识别对象类型属性

时间:2015-10-10 16:05:48

标签: c# .net reflection

我有这段代码:

var siteObject = getObjectModels(context, contractsIDList, date);
var clientObjSitesList = bridgeTable_clientObjModel(models, contracts, clients, siteObject);

private static IQueryable<object> bridgeTable_clientObjModel(
    IQueryable<ObjectModel> objModels,                                                     
    IQueryable<Contract> contracts,
    IQueryable<Client> client,
    IQueryable<SiteObject> siteObject)
{
    var clientObjSitesList = 
        from cn in contracts
        join cl in client on cn.Id equals cl.Id
        join so in siteObject on cn.Id equals so.ContractId
        select new
        {
            clientId = cl.Id,
            modelId = so.ModelId,
        });
}

private static object createClientTypesObjectList(
    IQueryable<Object> clientObjSitesList, 
    IQueryable<ObjectModel> models, 
    IQueryable<Contract> contracts, 
    IQueryable<Client> clients, 
    IQueryable<SiteObject> siteObject)
{
    var clientTypesObjectList = 
        (from cob in clientObjSitesList
         join om in models on cob.modelId equals om.Id into g
         from subpet in g.DefaultIfEmpty()
         select new
         {
             clientId = cob.clientId,
             modelId = cob.modelId,
             typeId = (subpet == null ? -1 : subpet.ObjectTypeId)

         }).ToList();
}

在此行中 createClientTypesObjectList()

 join om in models on cob.modelId equals om.Id into g

我收到此错误:

  

&#39;对象&#39;不包含&#39; modelId&#39;的定义没有扩展方法&#39; modelId&#39;接受类型&#39;对象&#39;的第一个参数。可以找到(你错过了使用指令或汇编引用吗?

据我所知,问题发生了,因为我在此函数中返回了对象类型:bridgeTable_clientObjModel()

任何想法如何解决问题?如何在 createClientTypesObjectList()函数中识别modelId属性?

2 个答案:

答案 0 :(得分:0)

&lt; clientObjSitesList&#39;中的元素是类型对象,没有modeId属性。你可以:

  1. 将参数类型更改为 IQueryable<YourType>clientObjSitesList

  2. 在查询中转换对象:

    join om in models on ((YourType)cob).modelId equals om.Id into g

  3. 使用动态作为您的类型。

答案 1 :(得分:0)

传递clientObjSitesLists参数时,将类型定义为IQueryable<Object>。当你迭代它时,你有一个Object,它没有属性。

private static object createClientTypesObjectList(
    IQueryable<Object> clientObjSitesList, // <- this line
    IQueryable<ObjectModel> models, 

您可能需要将此声明为IQueryable<SiteObject>或其他类型。