0

See the following example, now I need to check if UserService.GetByUserID() is null then return String.Empty. How can I still do this in one line?

var benefits = customerBenefits.Select(n =>
    new CustomerBenefit(n.BenefitID,
        n.AddedByUserID.HasValue 
            ? UserService.GetByUserID(n.AddedByUserID.Value).DisplayName 
            : n.AddedByAgentID, n.Reason);
3
  • 1
    Where do you want to return an empty string? It would be very odd to return either an empty string or a CustomerBenefit. Do you mean you need it for one of the constructor arguments? It would really help if you'd give more information. Commented Jun 24, 2014 at 6:27
  • I need to add another condition to check if the GetByUserID() return null then return emptystring else return DisplayName Commented Jun 24, 2014 at 6:30
  • And do you really want to use AddedByAgentId instead of a display name if there isn't an AddedByUserId? Don't you want to look up the agent's display name? Commented Jun 24, 2014 at 6:37

4 Answers 4

5
Call service first then assign result.

var benefits = customerBenefits
                        .Select(n => {
                            var user = serService.GetByUserID(n.AddedByUserID.Value);

                            return new CustomerBenefit(
                                n.BenefitID,                   
                                n.AddedByUserID.HasValue ? (user== null ? String.Empty : ).DisplayName) : n.AddedByAgentID,
                                n.Reason} );
Sign up to request clarification or add additional context in comments.

Comments

2

By using let in query syntax, you can store sub results:

var benefits = from n in customerBenefits
    let hasuserid = n.AddedByUserID.HasValue
    let user = hasuserid ? UserService.GetByUserID(n.AddedByUserID.Value) : null //the call to GetUserID is stored here
           select
                new CustomerBenefit(
                    n.BenefitID,                   
                    hasuserid 
                         ? (user == null ? string.Empty : user.DisplayName) //the stored sub result is reused
                         : n.AddedByAgentID,
                    n.Reason);

Comments

0

You can try something like that:

var benefits = customerBenefits.Select(n =>
        new CustomerBenefit(
            n.BenefitID,                   
            n.AddedByUserID.HasValue 
                ? (UserService.GetByUserID(n.AddedByUserID.Value) == null 
                    ? String.Empty 
                    : UserService.GetByUserID(n.AddedByUserID.Value).
                        DisplayName) 
            : n.AddedByAgentID,
            n.Reason);

1 Comment

I know but GetByUserID() gets called twice.
0

Is this what you need?

var benefits = from n in customerBenefits
    let id = n.AddedByUserID.HasValue
    let user = id ? UserService.GetByUserID(n.AddedByUserID.Value) : null            select
                new CustomerBenefit(
                    n.BenefitID,                   
                    id ? (user == null ? string.Empty : user.DisplayName)
                         : n.AddedByAgentID,
                    n.Reason);

1 Comment

It is what I want to do but I don't want GetByUserID() to be called twice.

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.