Reputation:
I have a LINQ query that I need to use a ternary operator on so certain joins are used based on certain criteria. So this is my query.
var lData = (from r in gServiceContext.CreateQuery("campaignresponse")
join a in gServiceContext.CreateQuery("activityparty") on ((EntityReference)r["activityid"]).Id equals ((EntityReference)a["activityid"]).Id
//tenary statement here
join c in gServiceContext.CreateQuery("contact") on ((EntityReference)a["partyid"]).Id equals c["contactid"]
where ((EntityReference)r["new_distributorid"]).Id.Equals(lProfileProperty.PropertyValue)
select new
{
});
This is what I want to do.
If r["new_distributorid"] == 1 I need to use:
join c in gServiceContext.CreateQuery("contact") on ((EntityReference)a["partyid"]).Id equals c["contactid"]
if r["new_distributorid"] == 2 then I need to use:
join c in gServiceContext.CreateQuery("account") on ((EntityReference)a["partyid"]).Id equals c["accountid"]
and if r["new_distributorid"] == 3 then I need to use:
join c in gServiceContext.CreateQuery("lead") on ((EntityReference)a["partyid"]).Id equals c["leadid"]
So basically is new_distributor == 1 I need to use a certain join if its a 2 I need another join and if its a 3 I need another join.
Is this possible? If it is, how would I go about setting that up?
Thanks!
Upvotes: 2
Views: 1581
Reputation: 203824
All that's changing is a single string value, so just determine that string value before you start defining the query:
string tableName = "";
switch(r["new_distributorid"])
{
case(1):
tableName = "contact";
case(2):
tableName = "account";
case(3):
tableName = "lead";
}
string tableID = tableName + "id";
//...
join c in gServiceContext.CreateQuery(tableName)
on ((EntityReference)a["partyid"]).Id equals c[tableID]
Upvotes: 3