5
\$\begingroup\$

I have a model object class like this

public class EventInterestsResponse
{
  public string id { get; set; }
  public int sortOrder { get; set; }
  public string name { get; set; }
  public string categoryName { get; set; }
  public string categoryId { get; set; }
}

And I am using it to hold some data like this

public List<EventInterestsResponse> GetEventInterests()
{

  _data = _rh.ExecuteGetRequest();
  var yourObject = _data != null ? (List<EventInterestsResponse>)jsonSerializer.Deserialize<List<EventInterestsResponse>>(_data) : null;
  return yourObject;
}

Later I want to preform some operations on this. For example, I want to check if any of the resulting objects contain a specific categoryId and if yes, I want to print it. I have written this working code:

foreach (var interest  in InterstList)
{
  if (interest.categoryId == "11")
  {

    <strong>Branche:@interest.name</strong> 
  }
  if (interest.categoryId == "22")
  {

    <strong>Udviklingsstadie:@interest.name</strong> 
  }
}

Now I wonder: Is this the most elegant way to do this? Maybe I don't even want to use a foreach loop. Can this be achieved by LINQ and using lambda expressions?

Can any one tell me how I can modify this code so that it uses LINQ and lambda expressions?

\$\endgroup\$
4
\$\begingroup\$

I'm not sure if this is any better and it might depend on how many if statements or different categories you have whether it's worth it. But an alternative could be something like:

var categoryLabelValues = new Dictionary<string, string>()
    {
        {"11", "Branche"},
        {"22", "Udviklingsstadie"}
    };

foreach(var interest in InterstList
                            .Where(p => categoryLabelValues.ContainsKey(p.categoryId)))
{ 
   <strong>@categoryLabelValues[interest.categoryId]:@interest.name</strong>
}
\$\endgroup\$
0
\$\begingroup\$

Some suggestions to improve your code:

There is a convention for naming things (see https://msdn.microsoft.com/en-us/library/ms229043(v=vs.110).aspx) Properties should be written in PascalCase

public class EventInterestsResponse
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string CategoryName { get; set; }
    public string CategoryId { get; set; }
    public int SortOrder { get; set; }
}

A better approch to returning null is returning an empty collection: it's safer for the caller (no need to check for null):

var list = GetEventInterests();
if (list != null) 
{
    foreach (var item in list) 
    {
        ...
    } 
}

VS

foreach (var item in GetEventInterests()) 
{
    ...
}

I'd change your method as follow:

public IList<EventInterestsResponse> GetEventInterests()
{
    var eventInterests = new List<EventInterestsResponse>();

    _data = _rh.ExecuteGetRequest();

    if (_data != null) 
    {
        eventInterests = (List<EventInterestsResponse>) jsonSerializer.Deserialize<List<EventInterestsResponse>>(_data);
    }  

    return eventInterests;
}

ps: What is a "EventInterests"? It sound strange to me, maybe we can find a better name ;-)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.