LINQ查询抛出异常 [英] LINQ Query throwing exception

查看:57
本文介绍了LINQ查询抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我以下LINQ查询在做什么错吗?

Hi can anyone advise me what i am doing wrong with the following LINQ query?

public ObjectResult<CompanyExecutives> GetCompanyExecutivesList()
{            
	// Check we have an ObjectContext
	if (entities == null) entities = new CompanySecretaryEntities();

	//define the query
        var query = from ce in entities.CompanyExecutives
                        join exec in entities.Executives on ce.ExecutiveID equals exec.ExecutiveID
                        join companies in entities.Companies on ce.CompanyID equals companies.CompanyID
                        select new
                        {
                            CompanyName = companies.CompanyName,
                            ExecutiveName = exec.ExecutiveName,
                            ExecutiveType = ce.ExecutiveType,
                            DateAppointment = ce.DateAppointment,
                            DateResignation = ce.DateResignation
                        };
							   

      // Create a query from the entityset
      ObjectQuery<CompanyExecutives> companyexecutives = (ObjectQuery<CompanyExecutives>)query;
			
      // Return the results
      return companyexecutives.Execute(MergeOption.AppendOnly);            
}


这是我得到的错误:

{System.InvalidCastException:无法转换类型为"System.Data.Objects.ObjectQuery`1 [<> f__AnonymousType1`5 [System.String,System.String,System.Int32,System.Nullable`1 [System .DateTime],System.Nullable`1 [System.DateTime]]]''键入"System.Data.Objects.ObjectQuery`1 [CompanySecretary.CompanyExecutives]".

在此先感谢


this is the error I am getting:

{System.InvalidCastException: Unable to cast object of type ''System.Data.Objects.ObjectQuery`1[<>f__AnonymousType1`5[System.String,System.String,System.Int32,System.Nullable`1[System.DateTime],System.Nullable`1[System.DateTime]]]'' to type ''System.Data.Objects.ObjectQuery`1[CompanySecretary.CompanyExecutives]''.

Thanks in advance

推荐答案

// Returns a list of all the CompanyExecutives entities from the database.
public ObjectResult GetCompanyExecutivesList()
{            
   // Check we have an ObjectContext
   if (entities == null) entities = new CompanySecretaryEntities();

   //define the query
   var query = from ce in entities.CompanyExecutives
		join exec in entities.Executives on ce.ExecutiveID equals exec.ExecutiveID
		join companies in entities.Companies on ce.CompanyID equals companies.CompanyID
		select new
		{
 		 CompanyName = companies.CompanyName,
		 ExecutiveName = exec.ExecutiveName,
		 ExecutiveType = ce.ExecutiveType,                         	                        
		 DateAppointment = ce.DateAppointment,
		 DateResignation = ce.DateResignation
		};

   // Create a query from the entityset
   ObjectQuery companyexecutives = (ObjectQuery)query;
			
   // Return the results
   return companyexecutives.Execute(MergeOption.AppendOnly);            
}



通过上述查询重新定义选择时,我以前返回的是CompanyExecutives对象类型.



I was previously returning an object type of CompanyExecutives when I was redefining my selection with the above query.


这篇关于LINQ查询抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆