0

I'm aiming to do a lamba expression with this query, and I'm stuck. I had to do some easy lambdas before and now I need to do a multiple condition mixing or and ands in this one and I'm new with this.

So let me explain a bit of a this structure and you will see why is tricky (at least for a newbie at lambda). I have Requests, where request have many attributes, attributes have name and value, because according to request State they have different attributes in this case RequestState.APPROVAL_PENDING is a constant that will bring the right state and attribute name have being also compared with constants with the attribute names I need and the value need find is wwid in the attribute value.

Then I will make a query.tolist(); and get all the requests, but I need to find out how to lambda this.

var query = from request in DBContext.REQUESTs
                        join attr in DBContext.REQUEST_ATTRIBUTES on request.REQUEST_ID equals attr.REQUEST_ID
                        where (attr.ATTRIBUTE_VALUE == wwid && 
                        (attr.ATTRIBUTE_NAME == RequestAttributes.APPROVER_2_WWID || attr.ATTRIBUTE_NAME == RequestAttributes.APPROVER_WWID))
                        && request.STATE_ID == RequestState.APPROVAL_PENDING
                        select request;
3
  • Have you seen this? What you have is more readable. Commented May 3, 2017 at 2:48
  • @daniel-vega. Did my answer work for you ? Commented May 13, 2017 at 6:23
  • @Boney, not really i changed my approach on that time Commented Dec 1, 2018 at 0:05

2 Answers 2

0
var query = DBContext.REQUESTs.Where(request => request.STATE_ID == RequestState.APPROVAL_PENDING)
                        .Join(DBContext.REQUEST_ATTRIBUTES.Where(attr => attr.ATTRIBUTE_VALUE == wwid &&
                                (attr.ATTRIBUTE_NAME == RequestAttributes.APPROVER_2_WWID || attr.ATTRIBUTE_NAME == RequestAttributes.APPROVER_WWID)),
                            request => request.REQUEST_ID,
                            attr => attr.REQUEST_ID,
                            (request, attr) => request);
Sign up to request clarification or add additional context in comments.

Comments

0

Here's another way of doing this.

Create a navigation property from Request to RequestAttributes:

 public class Request
{
    .... Properties...
    public virtual ICollection<RequestAttribute> RequestAttributes { get; set; }
}
public class RequestAttribute
{
    .... Properties...
    public virtual Request> Request { get; set; }
}

This creates a one to many relationship from Request to RequestAttributes. And your query becomes:

var query = DBContext.REQUESTs.Where(r=> r.REQUEST_ATTRIBUTES.Any(attr=>attr.ATTRIBUTE_VALUE == wwid && 
                    (attr.ATTRIBUTE_NAME == RequestAttributes.APPROVER_2_WWID || attr.ATTRIBUTE_NAME == RequestAttributes.APPROVER_WWID))
                    && r.STATE_ID == RequestState.APPROVAL_PENDING)
                    .Select(r=> r);

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.