• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1080
  • Last Modified:

Conditionally include joins in LINQ query

Is it possible to conditionally include joins in a LINQ query? Below is an example of my join query:

var myData = from d in dtMaster.AsEnumerable()
  join s1 in selected1 on d.Field<string>("memDescription") equals s1
  join s2 in selected2 on d.Field<string>("strSize") equals s2
  join s3 in selected3 on d.Field<string>("strCategory") equals s3
  select d;

What I would like to accomplish, is something like this:

var myData = from d in dtMaster.AsEnumerable()
  if Condition1 = true
    join s1 in selected1 on d.Field<string>("memDescription") equals s1
  if Condition2 = true
    join s2 in selected2 on d.Field<string>("strSize") equals s2
  if Condition3 = true
      join s3 in selected3 on d.Field<string>("strCategory") equals s3
  select d;

Or, could I build a string that represents the conditions and insert it into the query?

Thanks in advance!
0
tmorita
Asked:
tmorita
  • 3
  • 3
1 Solution
 
MonstahCommented:
Have you tried this ?

 
var myData = from d in dtMaster.AsEnumerable()
    where (condition1 == true)
            join s1 in selected1 on d.Field<string>("memDescription") equals s1

Open in new window

0
 
Fernando SotoCommented:
Hi tmorita;

You should be able to do what you want by building the query up in parts but in order for me to show how  to build the query you need to tell me the following:

What are the data types of selected1, selected2 and selected3 they seem to be simple types and NOT a data table?
If they are simple types and you are just trying to filter the results then you should be using a where clause.

Fernando
0
 
tmoritaAuthor Commented:
Hi Fernando,
They are datatables. Here's my code that creates them:
dView = (DataView)propertySelection1.gridView1.DataSource;
            dTable = dView.Table;
            var selected1 = (from sel in dTable.AsEnumerable()
                                where sel.Field<Boolean>(0) == true
                                select sel.Field<string>(1));
            int iCount = selected1.Count();
            if (iCount > 0 && iCount < dTable.Rows.Count)
            {
                HasSelections[0] = true;
            }

I found that if the datatables had no rows, this caused my query to return zero rows. Thus, the HasSelections boolean. So when HasSelections is false, I don't want to include that table in the query.

Looking forward to seeing what you come up with...



(BTW, Monstah, your solution didn't work. This returned 4,300 records:
            Boolean condition1 = true;
            Boolean condition2 = false;
            Boolean condition3 = false;            
            // Build the LINQ iEnumerable that will populate the temp table
            var myData = from d in dtMaster.AsEnumerable()
                         where (condition1 == true)
                         join s1 in selected1 on d.Field<string>("memDescription") equals s1
                         //where (condition2 == true)
                         //join s2 in selected2 on d.Field<string>("strSize") equals s2
                         //where (condition3 == true)
                         join s3 in selected3 on d.Field<string>("strCategory") equals s3
                       select d;

While this returned 0 records:

            Boolean condition1 = true;
            Boolean condition2 = false;
            Boolean condition3 = false;            
            // Build the LINQ iEnumerable that will populate the temp table
            var myData = from d in dtMaster.AsEnumerable()
                         where (condition1 == true)
                         join s1 in selected1 on d.Field<string>("memDescription") equals s1
                         //where (condition2 == true)
                         //join s2 in selected2 on d.Field<string>("strSize") equals s2
                         where (condition3 == true)
                         join s3 in selected3 on d.Field<string>("strCategory") equals s3
                       select d;
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
Fernando SotoCommented:
Hi tmorita;

The code snippet below should do what you want.

IEnumerable<DataRow> addJoin = null;
var myData = dtMaster.AsEnumerable();

if (Condition1 == true)
    addJoin = myData.Join( 
        selection1, 
        outter => outter.Field<String>( "memDescription" ), 
        inner => inner, 
        ( outter, inner ) => outter 
    );

if (Condition2 == true)
    addJoin = myData.Join( 
        selection2, 
        outter => outter.Field<String>( "strSize" ), 
        inner => inner, 
        ( outter, inner ) => outter 
    );

if (Condition3 == true)
    addJoin = myData.Join( 
        selection3, 
        outter => outter.Field<String>( "strCategory" ), 
        inner => inner, 
        ( outter, inner ) => outter 
    );
    
// Execute the query
var results = addJoin.ToList();

Open in new window


Fernando
0
 
tmoritaAuthor Commented:
Perfect, Fernando. Just what I was looking for. Thanks for the input.
0
 
tmoritaAuthor Commented:
Fernando included code in his solution. Copy and paste. Couldn't be easier!
0
 
Fernando SotoCommented:
Not a problem, glad I was able to help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now