linq to sql使用crm早期绑定类

时间:2013-11-01 16:33:18

标签: c# .net sql linq dynamics-crm-2011

我正在开发基于crm数据库的应用程序。我想使用linq to sql获取salesorder实体下的salesorderdetails数量。

我正在使用subselect查询。 salesorder实体有new_sefer字段。我的问题是

(from d in context.SalesOrderDetailSet
                where context.SalesOrderSet.Where(s => s.new_Sefer.Id == Id)
                .Select(i => i.SalesOrderId).Contains(d.SalesOrderId.Id)
                select d).Count();

它抛出异常。我还有关于子选择查询的问题

如何使用linq将此sql查询写入sql

select count(*) from salesorderdetail 
where salesorderId in (select salesorderId from salesorder 
                      where new_sefer = '750FEB6F-F742-E311-8F56-000C29F3049E')  

“无效”'where'条件。实体成员正在调用无效的属性或方法。“

at Microsoft.Xrm.Sdk.Linq.QueryProvider.ThrowException(Exception exception)    在Microsoft.Xrm.Sdk.Linq.QueryProvider.FindValidEntityExpression(Expression exp,String operation)    在Microsoft.Xrm.Sdk.Linq.QueryProvider.FindValidEntityExpression(Expression exp,String operation)    在Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhereMethodCall(MethodCallExpression mce,FilterExpressionWrapper parentFilter,Func 2 getFilter, BinaryExpression parent, Boolean negate) at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhereBoolean(String parameterName, Expression exp, FilterExpressionWrapper parentFilter, Func 2 getFilter,List 1 linkLookups, BinaryExpression parent, Boolean negate) at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhere(QueryExpression qe, String parameterName, Expression exp, List 1 linkLookups)    at Microsoft.Xrm.Sdk.Linq.QueryProvider.GetQueryExpression(Expression expression,Boolean& throwIfSequenceIsEmpty,Boolean& throwIfSequenceNotSingle,Projection& projection,NavigationSource& source,List 1& linkLookups) at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](Expression expression) at Microsoft.Xrm.Sdk.Linq.QueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.Count[TSource](IQueryable 1 source)    at Xrm.SalesOrderDetailOperations.SelectSalesOrderDetailBySeferId(XrmServiceContext context,Guid Id)在C:\ SVN \ Customers \ Burulas \ FlyDBOperations \ FlyDBOperations \ SalesOrderDetailOperations.cs:第26行    at FlyinOperationsTest.SalesOrderDetailOperationsSalesOrderDetailOperationsTest.SelectSalesOrderDetailBySeferIdSelectSalesOrderDetailBySeferIdTest()in C:\ SVN \ Customers \ Burulas \ FlyDBOperations \ FlyDBOperationsTest \ SalesOrderDetailOperationsSalesOrderDetailOperationsTest.cs:line 82

2 个答案:

答案 0 :(得分:6)

我相信这对你有用。

var count = (from d in context.SalesOrderDetailSet
                 join s in context.SalesOrderSet
                 on d.SalesOrderId.Id equals s.SalesOrderId
                 where s.new_Sefer.Id == Id
                 select d.SalesOrderId.Id).ToArray().Length;

Explination:

您正在编写的linq表达式实际上并没有转换为sql。它们被转换为CRM的查询API,因此您必须以CRM linq提供商可以解释的方式编写您的linq语句。这导致linq中存在的许多特性和方法不可用(如Count())。这是一个link to the SDK,其中列出了linq支持的功能。

因此,查询会根据salesorderid将salesorderdeatil加入salesorder记录,而salesorderid是salesorder实体的主键。然后在你的where子句中,你可以像你一样根据Id进行过滤。最后在select语句中我只选择了记录的ID。我这样做是因为它节省了带宽。如果不这样做,CRM将选择实体的每个属性,当您检索大文本字段时,它可能会导致性能问题。所以只检索你需要的东西。最后,由于不支持Count(),我只是将结果转换为数组并从中得到了长度。

答案 1 :(得分:-1)

from t in
(from t in db.salesorderdetail
where
    (from t0 in db.salesorderdetail
    where
      Convert.ToString(t0.new_sefer) == "750FEB6F-F742-E311-8F56-000C29F3049E"
    select new {
      t0.salesorderId
    }).Contains(new { t.salesorderId})
select new {
  Dummy = "x"
})
group t by new { t.Dummy } into g
select new {
  Column1 = (Int64?)g.Count()
}

我建议您寻找LINQER。它是一个将SQL QUERIES转换为LINQ的软件。

http://www.sqltolinq.com/downloads