Issue from SQL query case statement to a linq extension methods in C# Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Learning about LINQWhat are your favorite extension methods for C#? (codeplex.com/extensionoverflow)Case insensitive string compare in LINQ-to-SQLCode equivalent to the 'let' keyword in chained LINQ extension method callsWhere is the “Fold” LINQ Extension Method?LINQ-to-SQL Compiled Query Problem (works as uncompiled query)Why is the query operator 'ElementAt' is not supported in LINQ to SQL?Linq 2 SQL and custom extension methodLINQ query works agains SQL Server but not Entity Framework/LINQ to SQLCASE or IF Statement in Linq Query?

How many morphisms from 1 to 1+1 can there be?

What is the meaning of 'breadth' in breadth first search?

Significance of Cersei's obsession with elephants?

What is an "asse" in Elizabethan English?

Dynamic filling of a region of a polar plot

Can a new player join a group only when a new campaign starts?

Did any compiler fully use 80-bit floating point?

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

preposition before coffee

How to report t statistic from R

What does it mean that physics no longer uses mechanical models to describe phenomena?

Karn the great creator - 'card from outside the game' in sealed

Time evolution of a Gaussian wave packet, why convert to k-space?

Intuitive explanation of the rank-nullity theorem

Is there hard evidence that the grant peer review system performs significantly better than random?

Did Mueller's report provide an evidentiary basis for the claim of Russian govt election interference via social media?

What to do with repeated rejections for phd position

Semigroups with no morphisms between them

Do wooden building fires get hotter than 600°C?

One-one communication

Crossing US/Canada Border for less than 24 hours

Sum letters are not two different

Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?

What is the chair depicted in Cesare Maccari's 1889 painting "Cicerone denuncia Catilina"?



Issue from SQL query case statement to a linq extension methods in C#



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Learning about LINQWhat are your favorite extension methods for C#? (codeplex.com/extensionoverflow)Case insensitive string compare in LINQ-to-SQLCode equivalent to the 'let' keyword in chained LINQ extension method callsWhere is the “Fold” LINQ Extension Method?LINQ-to-SQL Compiled Query Problem (works as uncompiled query)Why is the query operator 'ElementAt' is not supported in LINQ to SQL?Linq 2 SQL and custom extension methodLINQ query works agains SQL Server but not Entity Framework/LINQ to SQLCASE or IF Statement in Linq Query?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I need help with the following SQL query to write in linq or extension methods query if both can provide both examples would be great for me.



This query is returning the last id entered by the user and will return an material id which is what I need for my c# program will use it.



SELECT TOP 1
CASE
WHEN s.Material = 'P4861VAPF' THEN 4578
WHEN s.Material = 'P2871VAPF' THEN 4579
WHEN s.Material = 'P2231VAPF' THEN 4580
ELSE NULL
END AS MaterialSelected
FROM
dbo.Shipments s
WHERE
s.material <> ''
ORDER BY
s.Id


I wrote this linq extension method in C# to re-create my SQL query is not working correctly is return the Id instead my select case statement



public int GetLastShipmentMaterialEntry()

var lastShipmentMaterialEntry = BarcodeReceivingDbContext.Shipments
.Where(s => s.Material != "")
.OrderBy(s => s.Id)
.Select(s =>
s.Material == "P4861VAPF" ? 4578 :
s.Material == "P2871VAPF" ? 4579 :
s.Material == "P2231VAPF" ? 4580 : 0)
.FirstOrDefault();

return lastShipmentMaterialEntry;



I have seen other query examples from other question here in stack overflow and google but not to exact of my current query. I also tried to use linqpad to see if there it could translate it but no luck so any help is appreciate it.










share|improve this question



















  • 1





    I would use TOP 1 and the order by s.Id instead of having to query the same again in the where clause.

    – Sean Lange
    Mar 8 at 22:26











  • Hello sean lange, the top 1 worked better thanks on that, but to translate my query to either extension method or linq you have an example?

    – NewCoder
    Mar 8 at 22:32











  • I updated the query example to use the top 1 and order by id but again to write it extension method or linq

    – NewCoder
    Mar 8 at 22:36











  • I updated my body description forgot to add my c# linq query extension I wrote to translate the sql query

    – NewCoder
    Mar 9 at 2:01











  • I was able to resolve my issue and I was able to create linq extension method based on my SQL Query, thanks all.

    – NewCoder
    Mar 9 at 23:15


















0















I need help with the following SQL query to write in linq or extension methods query if both can provide both examples would be great for me.



This query is returning the last id entered by the user and will return an material id which is what I need for my c# program will use it.



SELECT TOP 1
CASE
WHEN s.Material = 'P4861VAPF' THEN 4578
WHEN s.Material = 'P2871VAPF' THEN 4579
WHEN s.Material = 'P2231VAPF' THEN 4580
ELSE NULL
END AS MaterialSelected
FROM
dbo.Shipments s
WHERE
s.material <> ''
ORDER BY
s.Id


I wrote this linq extension method in C# to re-create my SQL query is not working correctly is return the Id instead my select case statement



public int GetLastShipmentMaterialEntry()

var lastShipmentMaterialEntry = BarcodeReceivingDbContext.Shipments
.Where(s => s.Material != "")
.OrderBy(s => s.Id)
.Select(s =>
s.Material == "P4861VAPF" ? 4578 :
s.Material == "P2871VAPF" ? 4579 :
s.Material == "P2231VAPF" ? 4580 : 0)
.FirstOrDefault();

return lastShipmentMaterialEntry;



I have seen other query examples from other question here in stack overflow and google but not to exact of my current query. I also tried to use linqpad to see if there it could translate it but no luck so any help is appreciate it.










share|improve this question



















  • 1





    I would use TOP 1 and the order by s.Id instead of having to query the same again in the where clause.

    – Sean Lange
    Mar 8 at 22:26











  • Hello sean lange, the top 1 worked better thanks on that, but to translate my query to either extension method or linq you have an example?

    – NewCoder
    Mar 8 at 22:32











  • I updated the query example to use the top 1 and order by id but again to write it extension method or linq

    – NewCoder
    Mar 8 at 22:36











  • I updated my body description forgot to add my c# linq query extension I wrote to translate the sql query

    – NewCoder
    Mar 9 at 2:01











  • I was able to resolve my issue and I was able to create linq extension method based on my SQL Query, thanks all.

    – NewCoder
    Mar 9 at 23:15














0












0








0








I need help with the following SQL query to write in linq or extension methods query if both can provide both examples would be great for me.



This query is returning the last id entered by the user and will return an material id which is what I need for my c# program will use it.



SELECT TOP 1
CASE
WHEN s.Material = 'P4861VAPF' THEN 4578
WHEN s.Material = 'P2871VAPF' THEN 4579
WHEN s.Material = 'P2231VAPF' THEN 4580
ELSE NULL
END AS MaterialSelected
FROM
dbo.Shipments s
WHERE
s.material <> ''
ORDER BY
s.Id


I wrote this linq extension method in C# to re-create my SQL query is not working correctly is return the Id instead my select case statement



public int GetLastShipmentMaterialEntry()

var lastShipmentMaterialEntry = BarcodeReceivingDbContext.Shipments
.Where(s => s.Material != "")
.OrderBy(s => s.Id)
.Select(s =>
s.Material == "P4861VAPF" ? 4578 :
s.Material == "P2871VAPF" ? 4579 :
s.Material == "P2231VAPF" ? 4580 : 0)
.FirstOrDefault();

return lastShipmentMaterialEntry;



I have seen other query examples from other question here in stack overflow and google but not to exact of my current query. I also tried to use linqpad to see if there it could translate it but no luck so any help is appreciate it.










share|improve this question
















I need help with the following SQL query to write in linq or extension methods query if both can provide both examples would be great for me.



This query is returning the last id entered by the user and will return an material id which is what I need for my c# program will use it.



SELECT TOP 1
CASE
WHEN s.Material = 'P4861VAPF' THEN 4578
WHEN s.Material = 'P2871VAPF' THEN 4579
WHEN s.Material = 'P2231VAPF' THEN 4580
ELSE NULL
END AS MaterialSelected
FROM
dbo.Shipments s
WHERE
s.material <> ''
ORDER BY
s.Id


I wrote this linq extension method in C# to re-create my SQL query is not working correctly is return the Id instead my select case statement



public int GetLastShipmentMaterialEntry()

var lastShipmentMaterialEntry = BarcodeReceivingDbContext.Shipments
.Where(s => s.Material != "")
.OrderBy(s => s.Id)
.Select(s =>
s.Material == "P4861VAPF" ? 4578 :
s.Material == "P2871VAPF" ? 4579 :
s.Material == "P2231VAPF" ? 4580 : 0)
.FirstOrDefault();

return lastShipmentMaterialEntry;



I have seen other query examples from other question here in stack overflow and google but not to exact of my current query. I also tried to use linqpad to see if there it could translate it but no luck so any help is appreciate it.







sql-server linq c#-4.0 extension-methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 7:11









marc_s

586k13011281272




586k13011281272










asked Mar 8 at 22:10









NewCoderNewCoder

4316




4316







  • 1





    I would use TOP 1 and the order by s.Id instead of having to query the same again in the where clause.

    – Sean Lange
    Mar 8 at 22:26











  • Hello sean lange, the top 1 worked better thanks on that, but to translate my query to either extension method or linq you have an example?

    – NewCoder
    Mar 8 at 22:32











  • I updated the query example to use the top 1 and order by id but again to write it extension method or linq

    – NewCoder
    Mar 8 at 22:36











  • I updated my body description forgot to add my c# linq query extension I wrote to translate the sql query

    – NewCoder
    Mar 9 at 2:01











  • I was able to resolve my issue and I was able to create linq extension method based on my SQL Query, thanks all.

    – NewCoder
    Mar 9 at 23:15













  • 1





    I would use TOP 1 and the order by s.Id instead of having to query the same again in the where clause.

    – Sean Lange
    Mar 8 at 22:26











  • Hello sean lange, the top 1 worked better thanks on that, but to translate my query to either extension method or linq you have an example?

    – NewCoder
    Mar 8 at 22:32











  • I updated the query example to use the top 1 and order by id but again to write it extension method or linq

    – NewCoder
    Mar 8 at 22:36











  • I updated my body description forgot to add my c# linq query extension I wrote to translate the sql query

    – NewCoder
    Mar 9 at 2:01











  • I was able to resolve my issue and I was able to create linq extension method based on my SQL Query, thanks all.

    – NewCoder
    Mar 9 at 23:15








1




1





I would use TOP 1 and the order by s.Id instead of having to query the same again in the where clause.

– Sean Lange
Mar 8 at 22:26





I would use TOP 1 and the order by s.Id instead of having to query the same again in the where clause.

– Sean Lange
Mar 8 at 22:26













Hello sean lange, the top 1 worked better thanks on that, but to translate my query to either extension method or linq you have an example?

– NewCoder
Mar 8 at 22:32





Hello sean lange, the top 1 worked better thanks on that, but to translate my query to either extension method or linq you have an example?

– NewCoder
Mar 8 at 22:32













I updated the query example to use the top 1 and order by id but again to write it extension method or linq

– NewCoder
Mar 8 at 22:36





I updated the query example to use the top 1 and order by id but again to write it extension method or linq

– NewCoder
Mar 8 at 22:36













I updated my body description forgot to add my c# linq query extension I wrote to translate the sql query

– NewCoder
Mar 9 at 2:01





I updated my body description forgot to add my c# linq query extension I wrote to translate the sql query

– NewCoder
Mar 9 at 2:01













I was able to resolve my issue and I was able to create linq extension method based on my SQL Query, thanks all.

– NewCoder
Mar 9 at 23:15






I was able to resolve my issue and I was able to create linq extension method based on my SQL Query, thanks all.

– NewCoder
Mar 9 at 23:15













1 Answer
1






active

oldest

votes


















1














I was able to re-create my SQL query using the Linq extension methods, if anyone thinks and sees a a better solution please let me know.



 public int GetLastShipmentMaterialEntry()

var lastShipmentMaterialEntry = BarcodeReceivingDbContext.Shipments
.Where(s => s.Material != "")
.OrderByDescending(s => s.Id)
.Select(s =>
s.Material == "P4861VAPF" ? 4578 :
s.Material == "P2871VAPF" ? 4579 :
s.Material == "P2231VAPF" ? 4580 : 0)
.FirstOrDefault();

return lastShipmentMaterialEntry;






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%2f55071703%2fissue-from-sql-query-case-statement-to-a-linq-extension-methods-in-c-sharp%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I was able to re-create my SQL query using the Linq extension methods, if anyone thinks and sees a a better solution please let me know.



     public int GetLastShipmentMaterialEntry()

    var lastShipmentMaterialEntry = BarcodeReceivingDbContext.Shipments
    .Where(s => s.Material != "")
    .OrderByDescending(s => s.Id)
    .Select(s =>
    s.Material == "P4861VAPF" ? 4578 :
    s.Material == "P2871VAPF" ? 4579 :
    s.Material == "P2231VAPF" ? 4580 : 0)
    .FirstOrDefault();

    return lastShipmentMaterialEntry;






    share|improve this answer



























      1














      I was able to re-create my SQL query using the Linq extension methods, if anyone thinks and sees a a better solution please let me know.



       public int GetLastShipmentMaterialEntry()

      var lastShipmentMaterialEntry = BarcodeReceivingDbContext.Shipments
      .Where(s => s.Material != "")
      .OrderByDescending(s => s.Id)
      .Select(s =>
      s.Material == "P4861VAPF" ? 4578 :
      s.Material == "P2871VAPF" ? 4579 :
      s.Material == "P2231VAPF" ? 4580 : 0)
      .FirstOrDefault();

      return lastShipmentMaterialEntry;






      share|improve this answer

























        1












        1








        1







        I was able to re-create my SQL query using the Linq extension methods, if anyone thinks and sees a a better solution please let me know.



         public int GetLastShipmentMaterialEntry()

        var lastShipmentMaterialEntry = BarcodeReceivingDbContext.Shipments
        .Where(s => s.Material != "")
        .OrderByDescending(s => s.Id)
        .Select(s =>
        s.Material == "P4861VAPF" ? 4578 :
        s.Material == "P2871VAPF" ? 4579 :
        s.Material == "P2231VAPF" ? 4580 : 0)
        .FirstOrDefault();

        return lastShipmentMaterialEntry;






        share|improve this answer













        I was able to re-create my SQL query using the Linq extension methods, if anyone thinks and sees a a better solution please let me know.



         public int GetLastShipmentMaterialEntry()

        var lastShipmentMaterialEntry = BarcodeReceivingDbContext.Shipments
        .Where(s => s.Material != "")
        .OrderByDescending(s => s.Id)
        .Select(s =>
        s.Material == "P4861VAPF" ? 4578 :
        s.Material == "P2871VAPF" ? 4579 :
        s.Material == "P2231VAPF" ? 4580 : 0)
        .FirstOrDefault();

        return lastShipmentMaterialEntry;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 10 at 15:27









        NewCoderNewCoder

        4316




        4316





























            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%2f55071703%2fissue-from-sql-query-case-statement-to-a-linq-extension-methods-in-c-sharp%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

            AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

            Алба-Юлія

            Захаров Федір Захарович