1

I have to fix a query which was already written in the LINQ Lambda, I found the fix in a Simple SQL Query but now I have some trouble in converting it to LINQ Query,

Here is my SQL Query

select * from RequestItem_SubRequestItem x 
where x.RequestItem_key = 1 and x.SubRequestItem_key in (
    select o.SubRequestItem_key 
    from SubRequestItem_Entitlement o 
    inner join SubRequestItem sr on sr.SubRequestItem_key = o.SubRequestItem_key  
    where o.Entitlement_key = 2 and sr.Action = 'Add' ) 

And below is my LINQ C# code where I am trying to insert my fixes which include inner join.

z.Entitlements = ARMContext.Context.SubRequestItem_Entitlement
    .Where(o => o.Entitlement_key == z.AccessKey && !o.Role_key.HasValue && o.Entitlement.EntitlementConfiguration.UserVisible == true
            && (ARMContext.Context.RequestItem_SubRequestItem
        .Where(x => x.RequestItem_key == requestItemKey)
        .Select(y => y.SubRequestItem_key)
        .Contains(o.SubRequestItem_key)))
        .Join(ARMContext.Context.SubRequestItems, subrq => subrq.SubRequestItem_key, temp => requestItemKey, (subrq, temp) => subrq == temp)

Where as previously the C# LINQ code looked like this

z.Entitlements = ARMContext.Context.SubRequestItem_Entitlement
    .Where(o => o.Entitlement_key == z.AccessKey && !o.Role_key.HasValue && o.Entitlement.EntitlementConfiguration.UserVisible == true
            && (ARMContext.Context.RequestItem_SubRequestItem
        .Where(x => x.RequestItem_key == requestItemKey)
        .Select(y => y.SubRequestItem_key)
        .Contains(o.SubRequestItem_key)))

When I try to insert the JOIN in the LINQ as per my conditions then I get to see this error. enter image description here

What is my mistake? Can anybody tell me a correct way to do it?

2
  • Add your model to the question. Do you have navigation properties? Commented Mar 1, 2021 at 12:14
  • @SvyatoslavDanyliv hi, I didnt get your question, but below mentioned solution did work properly. Commented Mar 1, 2021 at 13:29

2 Answers 2

1

I think this should Suffice your need, although you might have to make changes to the other code which are dependent on your SubRequestItem_Entitlement table with {user, add}

please have a look at that. As I am sure you will have to make those changes.

.Join(ARMContext.Context.SubRequestItems, user => user.SubRequestItem_key, subreqItems => subreqItems.SubRequestItem_key, (user, subreqItems) => new { user, subreqItems })
.Where(Action => Action.subreqItems.Action == z.ApprovalAction)
Sign up to request clarification or add additional context in comments.

Comments

1

you can use this query. I exactly matched the SQL query

var query = ARMContext.Context.RequestItem_SubRequestItem
     .Where(a => a.RequestItem_key == 1 && a.RequestItem_key == (ARMContext.Context.SubRequestItem_Entitlement
     .Join(ARMContext.Context.SubRequestItems,
     right => right.SubRequestItem_key,
     left => left.SubRequestItem_key,
     (right, left) => new
     {
        right = right,
        left = left
     })
     .Where(x => x.right.Entitlement_key == 2 && x.left.Action == "Add" && x.right.SubRequestItem_key == a.RequestItem_key).Select(y => y.right.SubRequestItem_key)).FirstOrDefault());

Comments

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.