How do I select, specify, or multi-column with SqlQuery in C# WPF Entity Framework?

Mojtaba_Hakim 301 Reputation points
2021-02-15T06:44:28.927+00:00

I'm using WPF, C#, Entity Framework database-first with a SQL Server database.

I did execute this query with successfully in SQL Server:

SELECT   
    SUM(MEGHk) AS Meghksm,   
    SUM(MABL_K) AS Mablksm,   
    SUM(N_MOIN) AS N_MOINSM,   
    SUM(IMBAA) AS IMBAAsm   
FROM   
    INVO_LST   
WHERE  
    (TAG = 2) AND (NUMBER = 4)  

68035-qeeph.png

In C# code:

 var quer_Sumfactor5 = dbms.Database.SqlQuery<INVO_LST>("SELECT SUM(MEGHk) AS Meghksm, SUM(MABL_K) AS Mablksm, SUM(N_MOIN) AS N_MOINSM, SUM(IMBAA) AS IMBAAsm FROM INVO_LST WHERE(TAG = 2) AND (NUMBER = " + TextB_Number.Text + ")").ToList();  

I get this error:

68045-zm9h9.png

I think Entity Framework needs all columns that won't select single or multi columns.

How can I get the result of my query as in SQL Server?

Please help

I hope my explanation is clear

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,863 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,778 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,938 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
809 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2021-02-16T06:59:12.517+00:00

    You get a new model with the SQL query, please create a new model for the query data.
    Create a new model named NewINVO_LST

     public class NewINVO_LST  
        {  
            public float Meghksm { get; set; }  
            public float floatMablksm { get; set; }  
            public float floatN_MOINSM { get; set; }  
            public float floatIMBAAsm { get; set; }  
        }  
    

    Then update your SQL Query:

    var quer_Sumfactor5 = dbms.Database.SqlQuery<NewINVO_LST>("SELECT SUM(MEGHk) AS Meghksm, SUM(MABL_K) AS Mablksm, SUM(N_MOIN) AS N_MOINSM, SUM(IMBAA) AS IMBAAsm FROM INVO_LST WHERE(TAG = 2) AND (NUMBER = " + TextB_Number.Text + ")").ToList();  
      
    

    It it doesn't work for you, please post here and I will try my best to help you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 117.6K Reputation points
    2021-02-15T08:42:14.53+00:00

    Try another SQL query:

    SELECT 
         NUMBER,
         SUM(MEGHk) AS Meghksm, 
         SUM(MABL_K) AS Mablksm, 
         SUM(N_MOIN) AS N_MOINSM, 
         SUM(IMBAA) AS IMBAAsm 
    FROM INVO_LST 
    WHERE (TAG = 2) AND (NUMBER = 4)
    GROUP BY NUMBER
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.