Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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
C# Linq到sql查询:如何防止代码重复_C#_.net_Linq_Linq To Sql_Refactoring - Fatal编程技术网

C# Linq到sql查询:如何防止代码重复

C# Linq到sql查询:如何防止代码重复,c#,.net,linq,linq-to-sql,refactoring,C#,.net,Linq,Linq To Sql,Refactoring,我的问题 我是Linq的新手,使用它时会遇到困难。我已经编写了函数查询,但是我不得不在每个查询中重复一些代码。查询的第一部分只是给出数据库的结构并删除损坏的数据,因此它总是相同的,并且不希望在我的代码中有多个版本 我试过的 我创建了一个返回查询部分的函数,但它不会编译,只是给出了一个意外的令牌错误,所以我丢失了 我的代码 问题: 如何编写查询,以便在代码中只编写一次公共部分?您可以拆分查询。首先-选择包含所有链接实体的匿名对象: var query = from leads i

我的问题

我是Linq的新手,使用它时会遇到困难。我已经编写了函数查询,但是我不得不在每个查询中重复一些代码。查询的第一部分只是给出数据库的结构并删除损坏的数据,因此它总是相同的,并且不希望在我的代码中有多个版本

我试过的

我创建了一个返回查询部分的函数,但它不会编译,只是给出了一个意外的令牌错误,所以我丢失了

我的代码

问题:


如何编写查询,以便在代码中只编写一次公共部分?

您可以拆分查询。首先-选择包含所有链接实体的匿名对象:

 var query = 
       from leads in dc.T_DM_FactDemandeWebLeads
       join demands in dc.T_DM_DimDemandeWebs 
            on leads.DemandeWeb_FK equals demands.DemandeWeb_PK
       join temps in dc.T_DM_Temps 
            on demands.DateDemande_FK equals temps.Temps_PK
       join distributeurs in dc.T_DM_DimDistributeurs 
            on leads.Distributeur_FK equals distributeurs.Distributeur_PK
       join geographies in dc.T_DM_DimGeographies 
            on distributeurs.DistributeurGeographie_FK equals geographies.Geographie_PK
       join typologies in dc.T_DM_DimTypologies 
            on leads.Typologie_FK equals typologies.Typologie_PK
       where (dc.ISNUMERIC(leads.GeolocDistanceRouteDistrib) == true) &&
              leads.longitudeClient != null && typologies.CodeProcessus == "LEAD"
       where (dc.ISNUMERIC(distributeurs.DistribIdPointDeVente) == true) 
       select new { 
           leads, 
           demands, 
           temps, 
           distributeurs,
           geographies,
           typologies
       };
第二个-编写特定查询:

  var leads = from x in query
              where (x.temps.Date > new DateTime(2013, 4, 1).Date)
              where (x.temps.Date < new DateTime(2013, 5, 30).Date)
              select new Lead {
                  id = Convert.ToInt32(x.leads.DemandeWeb_FK),
              });

您可以拆分查询。首先-选择包含所有链接实体的匿名对象:

 var query = 
       from leads in dc.T_DM_FactDemandeWebLeads
       join demands in dc.T_DM_DimDemandeWebs 
            on leads.DemandeWeb_FK equals demands.DemandeWeb_PK
       join temps in dc.T_DM_Temps 
            on demands.DateDemande_FK equals temps.Temps_PK
       join distributeurs in dc.T_DM_DimDistributeurs 
            on leads.Distributeur_FK equals distributeurs.Distributeur_PK
       join geographies in dc.T_DM_DimGeographies 
            on distributeurs.DistributeurGeographie_FK equals geographies.Geographie_PK
       join typologies in dc.T_DM_DimTypologies 
            on leads.Typologie_FK equals typologies.Typologie_PK
       where (dc.ISNUMERIC(leads.GeolocDistanceRouteDistrib) == true) &&
              leads.longitudeClient != null && typologies.CodeProcessus == "LEAD"
       where (dc.ISNUMERIC(distributeurs.DistribIdPointDeVente) == true) 
       select new { 
           leads, 
           demands, 
           temps, 
           distributeurs,
           geographies,
           typologies
       };
第二个-编写特定查询:

  var leads = from x in query
              where (x.temps.Date > new DateTime(2013, 4, 1).Date)
              where (x.temps.Date < new DateTime(2013, 5, 30).Date)
              select new Lead {
                  id = Convert.ToInt32(x.leads.DemandeWeb_FK),
              });

它会合并到sql请求中吗?或者第二个查询将在C中运行?@LSA它将是单个查询。这只是查询定义。在foreach中调用ToList、Count或枚举之类的函数之前,不会执行查询。您可以使用SQL Server或EF ProfilerTX的性能工具之一SQL Profiler对其进行验证。我仍然在声明datacontext时遇到问题,它说无法解析符号var@LSA数据上下文应该在查询定义之前声明和创建。类似var dc=newyourdatacontexti的东西我修复了它。谢谢你的帮助。它会合并到sql请求中吗?或者第二个查询将在C中运行?@LSA它将是单个查询。这只是查询定义。在foreach中调用ToList、Count或枚举之类的函数之前,不会执行查询。您可以使用SQL Server或EF ProfilerTX的性能工具之一SQL Profiler对其进行验证。我仍然在声明datacontext时遇到问题,它说无法解析符号var@LSA数据上下文应该在查询定义之前声明和创建。类似var dc=newyourdatacontexti的东西我修复了它。谢谢你的帮助。