简体   繁体   中英

Linq to sql query: how to prevent duplication of code

My problem

I'm very new to Linq and I have to difficulties using it. I have written functional queries but I was forced to duplicate some code in every single query. The first part for the queries is just there to give the structure of the database and remove corrupt data, so it always the same and it don't want to have several versions in my code.

What I tried

I made a function returning the portion of the query, but it won't compile and just gives an unexpected token error, so I'm lost.

My code

//always the same in each query : beginning
     IQueryable<Lead> query = (from costumers in dc.T_costumers
                                      join demands in dc.T_Demands on costumers.Costumer_FK equals typo.Typoe_PK
                                      where
                                          (dc.ISNUMERIC(costumers.Geoloc) == true) &&
                                          costumers.longitudeClient != null 
                                      where (dc.ISNUMERIC(shop.id) == true) 
//always the same in each query : end
                                       where (temps.Date > new DateTime(2013, 4, 1).Date)
                               select new Lead
                                           {
                                               id = Convert.ToInt32(costumers.id),

                                           });

Question

How do I wrote my queries so the common part is written only once in my code?

You can split query. First - select anonymous object with all linked entities:

 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
       };

Second - write specific query:

  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),
              });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM