Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:55:54+00:00 2026-06-09T02:55:54+00:00

I want to use a SQL query in Entity Framework. Here is my code:

  • 0

I want to use a SQL query in Entity Framework.

Here is my code:

 string sql = (@"SELECT G.GainId,
                (SELECT Name FROM Carrier WHERE CarrierId = G.CarrierId) AS Carrier,
                (SELECT Name + ' ' + Surname FROM [User] WHERE UserId = G.GainerId) AS Gainer,
                (SELECT Name + ' ' + Surname FROM [User] WHERE UserId = G.GiverId) AS Giver,
                (SELECT Name + ' ' + Surname FROM [User] WHERE UserId = G.CustomerId) AS Customer,
                    P.Name,
                    G.Gained,
                    G.Paid
                    FROM Gain AS G 
                    INNER JOIN Product AS P ON P.ProductId = G.ProductId");

            DataTable tbl = _context.Database.SqlQuery<DataTable>(sql) as DataTable;

But tbl is null. Do you have any suggestions? I am new with Entity Framework.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:55:55+00:00Added an answer on June 9, 2026 at 2:55 am

    That’s because SqlQuery is not meant to return a DataTable. It’s built to return your entity types as an IEnumerable<T> where T is your entity type. Use the below statement and remember that it’s an enumerable type that’s being returned. Now perform with it what you need.

    var tbl = _context.Database.SqlQuery<{your entity type}>(sql)
    

    Edited

    Yes, in fact you can very easily build this type. In fact, because the query seems so isolated you could probably build a private class inside of the class it’s executed in so that how I’ll build the example. However, that’s something you need to decide. Below I’ll give you a template for the class. The two properties with ?? I don’t know exactly what type they correlate to so you’ll need to plug those in.

    private class QueryResult
    {
        public int GainId { get; set; }
        public string Carrier { get; set; }
        public string Gainer { get; set; }
        public string Giver { get; set; }
        public string Customer { get; set; }
        public string Name { get; set; }
        public ?? Gained { get; set; }
        public ?? Paid { get; set; }
    }
    

    And then issue the statement like this…

    var tbl = _context.Database.SqlQuery<QueryResult>(sql)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a query like: sql(select name where id > 10 and
I query-ing this from entity framework using WCF, and i want to use the
I want to use this SQL query to get only the records between 8
I use jsp and sql and I want to query a table using a
I have a web application that is using Entity Framework to query a SQL
I use the following query to find dependencies on a view SELECT referenced_entity_name,* FROM
I want to use sql JOIN with COUNT in a way that using the
I'm writing a simple C# program and want to use an SQL server to
I use SQL Server 2008 R2 and have a table that I want no
I want to use the pl/sql case when statement like in java or c++.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.