3

I have following classes:

public class AuthenticateCustomerResponse
{
    public EligiblePromotions EligiblePromotions { get; set; }
}

public class EligiblePromotions
{
    public List<PromotionList> PromotionList { get; set; }
}
public class PromotionList
{
    public string LineOfBusiness { get; set; }
    public AuthenticatePromotions Promotions { get; set; }
}
public class AuthenticatePromotions
{
    public List<AuthenticatePromotion> Promotion { get; set; }
}
public class AuthenticatePromotion
{
    public string ServiceType { get; set; }
}

I want to retrieve PromotionList whose LineOfBusiness is equal to "VID" and ServiceType is equal to "SAT". I tried following lambda expression:

PromotionList getPromotion = AuthenticateCustomerResponse.EligiblePromotions.PromotionList.Find(p => p.LineOfBusiness.ToUpper().Equals("VID")).Promotions.Promotion.Find(o=>o.ServiceType.Equals("SAT"));

but I'm getting error:

Cannot implicitly convert type 'AuthenticatePromotion' to 'PromotionList'

What am I doing wrong?

3
  • The naming of your properties and class names is terrible. Use FirstOrDefault or Where it will be easier. Commented Dec 2, 2016 at 10:09
  • @Silvermind Post edited Commented Dec 2, 2016 at 10:10
  • You are retrieving an AuthenticatePromotion object from your Promotion list. If you only want the PromotionList, why do you have another Find? Commented Dec 2, 2016 at 10:11

2 Answers 2

8

The code below will get you the first object whose LineOfBusiness equals to 'VID'.

var promotionList = AuthenticateCustomerResponse.EligiblePromotions.PromotionList
.Where(p => p.LineOfBusiness.ToUpper().Equals("VID")).FirstOrDefault();

You can use .ToList() if you want all objects that have VID as LineOfBusiness instead of .FirstOrDefault()

Edit: You can add another clause to your .Where() if you want to check for the SAT

Sign up to request clarification or add additional context in comments.

3 Comments

Cannot implicitly convert type 'System.Collections.Generic.List<PromotionList>' to 'PromotionList'
ToList returns in a list of objects which pass your Where clause. If you only want one object, select one from the list or use FirstOrDefault
Just a tip: I would rethink the way you have implemented your classes. It doesn't make much sense to create a reference to a class which only contains a list. You could make your life a lot easier and move the list to PromotionList
1

This part of your code

.Promotion.Find(o=>o.ServiceType.Equals("SAT"));

Returns an AuthenticatePromotion, but you are explicitly asking for a PromotionList object.

One way to get rid of the error would be to use the implicit variable keyword var:

var getPromotion = AuthenticateCustomerResponse.EligiblePromotions.PromotionList.Find(p => p.LineOfBusiness.ToUpper().Equals("VID")).Promotions.Promotion.Find(o=>o.ServiceType.Equals("SAT"));

But then this will of course still return an AuthenticatePromotion

2 Comments

Maybe my query is wrong. Do you think the query is correct to retrieve PromotionList whose LineOfBusiness is equal to "VID" and ServiceType is equal to "SAT ?
@zariakhan I think that may depend on the rest of your application, although I must say with such a long line I would recommend placing the entire query into a method with a sensible name to improve readability

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.