question

MojtabaHakim-8125 avatar image
0 Votes"
MojtabaHakim-8125 asked ·

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

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


dotnet-csharpwindows-wpfdotnet-wpf-xamldotnet-entity-framework
qeeph.png (6.4 KiB)
zm9h9.png (20.1 KiB)
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Here is my structure :
68055-untitled.png


0 Votes 0 ·
untitled.png (4.3 KiB)
Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

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
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Plesae help me about this : How do I get the result of SQL Query in C# Entity Framework?

0 Votes 0 ·
DaisyTian-MSFT avatar image
0 Votes"
DaisyTian-MSFT answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.