if Statement within List View Controller Method

I have the following code within my controller method to provide data to the List View of membership Types:

[HttpGet]
public ActionResult SelectMembershipType(int clubId)
    {
        //Populate the list
        var membershipTypes = from s in db.MembershipTypes
                              where (s.ClubId == clubId  && s.Dormant == false)
                              orderby s.Type
                              select s;

        //Generate the ViewModel with the appropriate membership types
        var viewModel = membershipTypes.Select(t => new   SelectMembershipTypeViewModel
        {
            //Select appropriate Cost based on current month
            if(DateTime.Now.Month == t.ReducedMonth)
            {
                Cost = t.ReducedCost;
            }
            else
            {
                Cost = t.Cost;
            }
            ClubId = club.ClubId,
            Name = club.Name,              
            MembershipName = t.Type,
            MembershipType = t.MembershipTypeClassification.Type,
            MembershipTypeId = t.MembershipTypeId,
        });
        return View(viewModel);
    }

The if statement Ive put in doesn't work it throws several errors. I'm trying to apply an if Statement to the value for Cost i.e. if todays month is equal to the ReducedMonth in the database for each membership Type make the value of Cost equal to the Membership Types ReducedCost value, if not make it equal to its Cost instead. Each MembershipType can have different Reduced Months and Costs

I'm not sure of the correct syntax to code this correctly

Jon Skeet
people
quotationmark

The if statement Ive put in doesn't work it throws several errors.

Yes, it would - you've put it in an object initializer. Object initializers aren't arbitrary blocks of code - they're a list of property = value assignments, basically. (Slightly more to it than that, but...)

Fortunately, in this case you can use the conditional operator instead:

var viewModel = membershipTypes.Select(t => new SelectMembershipTypeViewModel
{
    //Select appropriate Cost based on current month
    Cost = DateTime.Now.Month == t.ReducedMonth ? t.ReducedCost : t.Cost,
    ClubId = club.ClubId,
    Name = club.Name,              
    MembershipName = t.Type,
    MembershipType = t.MembershipTypeClassification.Type,
    MembershipTypeId = t.MembershipTypeId,
});

That will compile, but it might not work due to the conversion of the query to SQL. You may need to extract the DateTime.Now.Month part first:

int month = Date.Now.Month;
var viewModel = membershipTypes.Select(t => new SelectMembershipTypeViewModel
{
    //Select appropriate Cost based on current month
    Cost = t.ReducedMonth == month ? t.ReducedCost : t.Cost,
    ClubId = club.ClubId,
    Name = club.Name,              
    MembershipName = t.Type,
    MembershipType = t.MembershipTypeClassification.Type,
    MembershipTypeId = t.MembershipTypeId,
});

Note that DateTime.Now.Month will be the month in the time zone of the server here... is that definitely what you want? Assuming you want it to be in a particular time zone, I would make that explicit... otherwise you could run into some hard-to-diagnose problems.

people

See more on this question at Stackoverflow