1

I have two tables that I want to join using a linq query

Table 1 called activities has the following fields ID Code Description

Table 2 named pred has the following fields task_id pred_task_id pred_type

I want the linq query to join both tables and returns values if and only if both task_id and pred_task_id exists in Table 1.ID

I had a stab at it with no success

from pre in pred.AsEnumerable()
join act in activities.AsEnumerable()
on pre.Field<string>("task_id") equals act.Id
where (pre.Field<string>("task_id") == activity.Id)
select new
{
    Pred = getTaskbyID(pre.Field<string>("pred_task_id"), activities),
    Succ = getTaskbyID(pre.Field<string>("task_id"),activities),
    RelationshipType = pre.Field<string>("pred_type")

});

This code does not execute what I am aiming to achieve. It returns list of all activities if the pred.task_id exist in activities.id. I want to make another filter to ensure that pred.pred_task_id also exists in the activities table.

4
  • Make another join to activities on pred.pred_task_id? Commented Dec 16, 2016 at 16:33
  • If you're making that JOIN, won't pre.Field<string>("task_id") already equal activity.Id? Commented Dec 16, 2016 at 16:37
  • I have tried adding another join which did not resulted in achieving my objectives. Commented Dec 17, 2016 at 7:41
  • Robert, Yes the join will make task_id equal to activity.Id but what I want to achieve is a bit different. I want to join tables is the above condition is met and if pred_task_id which is another field values exist in activity.Id Commented Dec 17, 2016 at 7:44

1 Answer 1

1

You need to change your where clause:

where activities.Any(a=> a.Id == pre.Field<string>("pred_task_id"))
Sign up to request clarification or add additional context in comments.

1 Comment

Mark, I have tried it and your code is working exactly as it is expected to perform. Many thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.