Select new with an if statement inside












1















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();
}









share|improve this question

























  • I have no idea what you want. You want to filter by info_name, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what your if suggests)?

    – Rango
    Nov 28 '18 at 9:39


















1















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();
}









share|improve this question

























  • I have no idea what you want. You want to filter by info_name, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what your if suggests)?

    – Rango
    Nov 28 '18 at 9:39
















1












1








1








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();
}









share|improve this question
















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();
}






c# .net linq if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 10:32









S.Akbari

30.5k93774




30.5k93774










asked Nov 28 '18 at 9:36









Johan JönssonJohan Jönsson

1048




1048













  • I have no idea what you want. You want to filter by info_name, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what your if suggests)?

    – Rango
    Nov 28 '18 at 9:39





















  • I have no idea what you want. You want to filter by info_name, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what your if suggests)?

    – Rango
    Nov 28 '18 at 9:39



















I have no idea what you want. You want to filter by info_name, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what your if suggests)?

– Rango
Nov 28 '18 at 9:39







I have no idea what you want. You want to filter by info_name, that only records != "BadName" are returned? or you want to return this list only if there is no badname in the table (what your if suggests)?

– Rango
Nov 28 '18 at 9:39














3 Answers
3






active

oldest

votes


















2














You need to add a where statement here:



info = (from j in db.info_list()
where j.info_name != "BadName"
select new Info
{
InfoName = j.info_name,
InfoId = j.info_id,
InfoValue = j.info_value,
}).ToList();





share|improve this answer

































    2














    Or by using Where extension method



    var goodNames = 
    db.info_list().Where(info => info.info_name != "Badname")
    .Select(info => new Info
    {
    InfoName = info.info_name,
    InfoId = info.info_id,
    InfoValue = info.info_value,
    })
    .ToList();





    share|improve this answer































      2














      You may use a where to filter the info as follow:-



      info = (from j in db.info_list()
      where j.info_name != "BadName"
      select new Info
      {
      InfoName = j.info_name,
      InfoId = j.info_id,
      InfoValue = j.info_value,
      }).ToList();





      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516309%2fselect-new-with-an-if-statement-inside%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        You need to add a where statement here:



        info = (from j in db.info_list()
        where j.info_name != "BadName"
        select new Info
        {
        InfoName = j.info_name,
        InfoId = j.info_id,
        InfoValue = j.info_value,
        }).ToList();





        share|improve this answer






























          2














          You need to add a where statement here:



          info = (from j in db.info_list()
          where j.info_name != "BadName"
          select new Info
          {
          InfoName = j.info_name,
          InfoId = j.info_id,
          InfoValue = j.info_value,
          }).ToList();





          share|improve this answer




























            2












            2








            2







            You need to add a where statement here:



            info = (from j in db.info_list()
            where j.info_name != "BadName"
            select new Info
            {
            InfoName = j.info_name,
            InfoId = j.info_id,
            InfoValue = j.info_value,
            }).ToList();





            share|improve this answer















            You need to add a where statement here:



            info = (from j in db.info_list()
            where j.info_name != "BadName"
            select new Info
            {
            InfoName = j.info_name,
            InfoId = j.info_id,
            InfoValue = j.info_value,
            }).ToList();






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 '18 at 9:44

























            answered Nov 28 '18 at 9:38









            S.AkbariS.Akbari

            30.5k93774




            30.5k93774

























                2














                Or by using Where extension method



                var goodNames = 
                db.info_list().Where(info => info.info_name != "Badname")
                .Select(info => new Info
                {
                InfoName = info.info_name,
                InfoId = info.info_id,
                InfoValue = info.info_value,
                })
                .ToList();





                share|improve this answer




























                  2














                  Or by using Where extension method



                  var goodNames = 
                  db.info_list().Where(info => info.info_name != "Badname")
                  .Select(info => new Info
                  {
                  InfoName = info.info_name,
                  InfoId = info.info_id,
                  InfoValue = info.info_value,
                  })
                  .ToList();





                  share|improve this answer


























                    2












                    2








                    2







                    Or by using Where extension method



                    var goodNames = 
                    db.info_list().Where(info => info.info_name != "Badname")
                    .Select(info => new Info
                    {
                    InfoName = info.info_name,
                    InfoId = info.info_id,
                    InfoValue = info.info_value,
                    })
                    .ToList();





                    share|improve this answer













                    Or by using Where extension method



                    var goodNames = 
                    db.info_list().Where(info => info.info_name != "Badname")
                    .Select(info => new Info
                    {
                    InfoName = info.info_name,
                    InfoId = info.info_id,
                    InfoValue = info.info_value,
                    })
                    .ToList();






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 28 '18 at 9:40









                    FabioFabio

                    20.1k22048




                    20.1k22048























                        2














                        You may use a where to filter the info as follow:-



                        info = (from j in db.info_list()
                        where j.info_name != "BadName"
                        select new Info
                        {
                        InfoName = j.info_name,
                        InfoId = j.info_id,
                        InfoValue = j.info_value,
                        }).ToList();





                        share|improve this answer






























                          2














                          You may use a where to filter the info as follow:-



                          info = (from j in db.info_list()
                          where j.info_name != "BadName"
                          select new Info
                          {
                          InfoName = j.info_name,
                          InfoId = j.info_id,
                          InfoValue = j.info_value,
                          }).ToList();





                          share|improve this answer




























                            2












                            2








                            2







                            You may use a where to filter the info as follow:-



                            info = (from j in db.info_list()
                            where j.info_name != "BadName"
                            select new Info
                            {
                            InfoName = j.info_name,
                            InfoId = j.info_id,
                            InfoValue = j.info_value,
                            }).ToList();





                            share|improve this answer















                            You may use a where to filter the info as follow:-



                            info = (from j in db.info_list()
                            where j.info_name != "BadName"
                            select new Info
                            {
                            InfoName = j.info_name,
                            InfoId = j.info_id,
                            InfoValue = j.info_value,
                            }).ToList();






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 28 '18 at 9:46

























                            answered Nov 28 '18 at 9:39









                            Afnan AhmadAfnan Ahmad

                            1,89121533




                            1,89121533






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516309%2fselect-new-with-an-if-statement-inside%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                Calculate evaluation metrics using cross_val_predict sklearn

                                Insert data from modal to MySQL (multiple modal on website)