如何针对CRM 2011编译LINQ查询?
我使用Microsoft.Xrm.Client.dll连接到CRM 2011,并使用从app.config文件中的连接字符串生成的Microsoft.Xrm.Client.CrmOrganizationServiceContext _SvcContext。我有以下LINQ查询,它很有用:
var vdiConfigDataSet =
(from SvrToPubProfSet in _SvcContext.vdi_publicationprofile_vdi_serverSet
join PubProfSet in _SvcContext.vdi_publicationprofileSet
on SvrToPubProfSet.vdi_publicationprofileid equals PubProfSet.Id
join SvrSet in _SvcContext.vdi_serverSet
on SvrToPubProfSet.vdi_serverid equals SvrSet.Id
where SvrSet.vdi_address == System.Environment.MachineName // This will ignore case
select new
{
// Data from Server record
RootPath = SvrSet.vdi_rootpath, // This is the same for all Publication Profiles
SvrAddr = SvrSet.vdi_address,
// Data from N:N relationship record(s) vdi_publicationprofile_vdi_serverSet
SvrId = SvrToPubProfSet.vdi_serverid,
PubProfId = SvrToPubProfSet.vdi_publicationprofileid,
// Data from Publication Profile record(s)
PubProfName = PubProfSet.vdi_name,
RelativePath = PubProfSet.vdi_relativepath,
ReportRelativePath = PubProfSet.vdi_reportrelativepath,
ReportFilenameSeed = PubProfSet.vdi_reportfilenameseed,
SubSystemType = PubProfSet.vdi_subsystemtype,
ModifiedOn = PubProfSet.ModifiedOn
}).ToList();
这是一个非常大的查询,可以反复执行。我想把它编译下来。我尝试过以下方法:
public static readonly Func<DataContext, Object> s_compiledQuery =
CompiledQuery.Compile<DataContext, Object>
(ctx =>
(from SvrToPubProfSet in ctx.GetTable<vdi_publicationprofile_vdi_server>()
join PubProfSet in ctx.GetTable<vdi_publicationprofile>()
on SvrToPubProfSet.vdi_publicationprofileid equals PubProfSet.Id
join SvrSet in ctx.GetTable<vdi_server>()
on SvrToPubProfSet.vdi_serverid equals SvrSet.Id
where SvrSet.vdi_address == System.Environment.MachineName // This will ignore case
select new
{
// Data from Server record
RootPath = SvrSet.vdi_rootpath, // This is the same for all Publication Profiles
SvrAddr = SvrSet.vdi_address,
// Data from N:N relationship record(s) vdi_publicationprofile_vdi_serverSet
SvrId = SvrToPubProfSet.vdi_serverid,
PubProfId = SvrToPubProfSet.vdi_publicationprofileid,
// Data from Publication Profile record(s)
PubProfName = PubProfSet.vdi_name,
RelativePath = PubProfSet.vdi_relativepath,
ReportRelativePath = PubProfSet.vdi_reportrelativepath,
ReportFilenameSeed = PubProfSet.vdi_reportfilenameseed,
SubSystemType = PubProfSet.vdi_subsystemtype,
ModifiedOn = PubProfSet.ModifiedOn
}).ToList());
...
var vdiConfigDataSet2 = s_compiledQuery(_SvcContext); //ERROR _SvcContext is not DataContext type
但是我需要一个DataContext
类型的对象,我不知道如何从Microsoft.Xrm.Client.CrmOrganizationServiceContext
类型对象中获取它。
任何人都有任何想法如何在为CRM 2011编译LINQ查询时获得DataContext
或更优雅的解决方案?
答案 0 :(得分:0)
CRM服务上下文代理不是基于实体框架的数据上下文,而是“在幕后”是ODATA / SOAP。您无法编译CRM查询,因为CompileQuery
类泛型arg必须从ObjectContext
继承。
您可能想要研究的一件事是使用此参数: /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization,Microsoft.Xrm.Client.CodeGeneration“ 使用SDK的代码生成工具(crmsvcutil.exe),这样可以在CRM linq查询中遍历“导航属性”,从而减轻您的连接。