Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a c# win application and i would use navigation properties of linq only for some conditiones are applied.
for example i have this class

C#
public class t020
   {
       public decimal ID_CLIENTE { get; set; }
       public Nullable<decimal> ID_COMUNE { get; set; }
       public Nullable<decimal> ID_CONSORZIO { get; set; }
   }

with this navigation properties
C#
public partial class T232 
    {
        public decimal ID_CLIENTE_CONTO { get; set; }
        public Nullable<decimal> ID_SISTEMA { get; set; }
        public decimal ID_CLIENTE { get; set; }
     }
     public partial class T234 
    {
        public decimal ID_CONTO { get; set; }
        public decimal ID_CLIENTE { get; set; }
     }


i would use include of T232 and of t234 only some condition are applied for example using this variables:
string MyString =null
decimal MyDecimal =8
and so i would using include only when Mystring is not null to use Include of t232
and only when mydecimal =8 to use include of y234
i would have a query like this

C#
using (var cont = DALProvider.CreateEntityContext())
{
   var result = from da in dalProv.Include(i => i.T232 where MyString!=null)
                                  .Include(i => i.T23d where MyDecimal==8)
                select da
}

and so on but in this way linq gives me error.
how can i do this in linq?

thanks a lot
Posted
Comments
Maciej Los 20-Apr-15 11:07am    
What error message? What is your issue?

You can't specify the condition for the include as an argument to .Include(..).

Instead just perform the include conditionally:

C#
using (var cont = DALProvider.CreateEntityContext())
{
   var query = dalProv;

   if (MyString != null)
      query = query.Include(i => i.T232);

   if (MyDecimal == 8)
      query = query.Include(i => i.T234);

   // potentially further includes or where-predicates...
   // query = query.Where(...)

   return query;
}
 
Share this answer
 
v2
Well sure you can:



C#
using (var cont = DALProvider.CreateEntityContext())
{
   var query= dalProv.Include(i => i.T232 where MyString!=null)
   if(mydecimal =8)
        query = query.Include(i => i.T23d where MyDecimal==8)

  var result = from da in query
                select da
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900