Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 如何动态构建WCF查询?_Linq_Entity Framework_Entity_Wcf Data Services - Fatal编程技术网

Linq 如何动态构建WCF查询?

Linq 如何动态构建WCF查询?,linq,entity-framework,entity,wcf-data-services,Linq,Entity Framework,Entity,Wcf Data Services,我想构建一些linq,或者动态构建一个查询字符串并将其传递给WCF数据服务(使用实体框架数据模型) 大概是这样的: public List<DocumentInformationRecord> SearchClientDocs(string clientCode, string clientName, string contactName, string groupCode, string groupName, string file

我想构建一些linq,或者动态构建一个查询字符串并将其传递给WCF数据服务(使用实体框架数据模型)

大概是这样的:

 public List<DocumentInformationRecord> SearchClientDocs(string clientCode,
            string clientName, string contactName, string groupCode, string groupName,
            string filename, string createdby, DateTime dateFrom, DateTime dateTo)
 {
   List<DocumentInformationRecord> results = new List<DocumentInformationRecord>();
   if(!string.IsNullOrEmpty(clientCode)) 
   //Add the client code clause...
var qry = from item in someList
          select item;

if (nameFilter != null)
{
    qry = qry.Where(item => item.Name == nameFilter);
}

if (someOtherFilter != null)
{
    qry = qry.Where(item => item.SomeOtherStuff == someOtherFilter);
}
// and so on

有什么想法吗?我尝试了谓词生成器(),但出现了一些无效的操作异常….

我不确定我是否完全理解您的问题,但有时我会编写代码,根据输入使用不同的部分构建LINQ查询。通常是这样的:

 public List<DocumentInformationRecord> SearchClientDocs(string clientCode,
            string clientName, string contactName, string groupCode, string groupName,
            string filename, string createdby, DateTime dateFrom, DateTime dateTo)
 {
   List<DocumentInformationRecord> results = new List<DocumentInformationRecord>();
   if(!string.IsNullOrEmpty(clientCode)) 
   //Add the client code clause...
var qry = from item in someList
          select item;

if (nameFilter != null)
{
    qry = qry.Where(item => item.Name == nameFilter);
}

if (someOtherFilter != null)
{
    qry = qry.Where(item => item.SomeOtherStuff == someOtherFilter);
}
// and so on

这样您就可以一步一步地构建查询。您可以这样做,因为延迟执行;在您开始对结果进行迭代之前,不会对数据源执行查询。

不确定它是否适合您的情况,但您可以使用表达式树来构建动态查询。有一个

我使用构建的字符串和qry.AddQueryOption(“$filter”)方法构建了Dataservice查询。但是表达式树是最好的方法,就目前而言,使用$filter选项似乎更快。这很有效,但如果生成的查询必须是在运行时构建的and和or的长组合,则情况并非如此。