None
None

Reputation: 5670

Convert looping statement to linq mode

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 prform 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 some code and it works

  var InterstList = rb.GetEventInterests();
             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?

Upvotes: 2

Views: 102

Answers (2)

ekad
ekad

Reputation: 14614

I don't think you can remove the foreach loop, but you can use Enumerable.Where Method to filter InterstList so the foreach loop will only enumerate elements of InterstList with categoryId equals 11 or 22 instead of all elements.

var InterstList = rb.GetEventInterests();
foreach (var interest in InterstList.Where(x => x.categoryId == "11" 
    || x.categoryId == "22"))
{
    if (interest.categoryId == "11")
    {
        <strong>Branche:@interest.name</strong> 
    }
    if (interest.categoryId == "22")
    {
        <strong>Udviklingsstadie:@interest.name</strong> 
    }
}

UPDATE
If you want to check whether InterstList contains any element with categoryId equals 11 or 22 first, you can use Enumerable.Any Method:

if (InterstList.Any(x => x.categoryId == "11" || x.categoryId == "22"))
{
    // get the list that satisfies the same condition
    var filteredList = InterstList.Where(x => x.categoryId == "11" || x.categoryId == "22");

    // get item with categoryId = "11", assuming there's only one
    EventInterestsResponse item1 = InterstList.FirstOrDefault(x => x.categoryId == "11");

    // get item with categoryId = "22", assuming there's only one
    EventInterestsResponse item2 = InterstList.FirstOrDefault(x => x.categoryId == "22");
}

Upvotes: 2

brothers28
brothers28

Reputation: 1206

I thing with linq you will also have to make a loop. But the linq statement would be something like this:

    var InterstList = rb.GetEventInterests();
    foreach (var interset in IntersetList.Where(x => x.categoryId == "11" || x.categoryId == "22")
    {
        <strong>Branche:@interest.name</strong> 
        <strong>Udviklingsstadie:@interest.name</strong> 
    }

Upvotes: 1

Related Questions