mercredi 28 novembre 2018

c# select new with an if statement inside

I have this piece of code that select new some info from the database in my c# project but i only want it to do it if the name is not a certain name so here is the code

public List<Info> GetInfo(int id)
    {
        var cacheKey = "allinfo-" + id;
        var info = SoftCache.GetData<List<info>>(cacheKey);
        if (info != null)
            return info;

        using (var db = DB.InfoModel)
        {
            info = (from j in db.info_list()

                        select new Info
                        {
                            InfoName = j.info_name,
                            InfoId = j.info_id,
                            InfoValue = j.info_value,
                        }).ToList();
        }

        SoftCache.Add(cacheKey, info, new TimeSpan(0, 0, 5), new TimeSpan(0, 1, 0));
        return info;
    }

what i want is something like

if(j.info_name != "BadName"){
            select new Info
                            {
                                InfoName = j.info_name,
                                InfoId = j.info_id,
                                InfoValue = j.info_value,
                            }).ToList();
}

Sql is one of my weak areas tried with some case when but cant seem to get it to work with select new.

Aucun commentaire:

Enregistrer un commentaire