自定义API控制器不返回任何记录

时间:2015-08-04 15:24:19

标签: linq azure asp.net-apicontroller

我需要将sql查询的结果返回到我的移动应用程序。

我有一个关于天蓝色和移动服务的数据库。

我已经为我的服务添加了一个新的自定义API控制器,但我无法获得任何记录。我一直在阅读和阅读,但仍然不明白我错过了什么。任何帮助将不胜感激

using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Net;    
using System.Net.Http;    
using System.Web.Http;    
using Microsoft.WindowsAzure.Mobile.Service;    
using mytestService.Models;    
using mytestService.DataObjects;    
using System.Data.Common;    
using System.Data.SqlClient;    
using System.Data;    
using System.Threading.Tasks; 
namespace mytestService.Controllers    
{    
    public class SelectMapMarkersController : ApiController     
    {    
        public ApiServices Services { get; set; }    
        public class MapMarkers
        {
            public string MerchantName { get; set; }
            public double MerchantLongitude { get; set; }
            public double MerchantLatitude { get; set; }
            public string MerchantMiniURL { get; set; }
            public string Deal { get; set; }
            public string CategoryDescription { get; set; }
        }   

        // GET api/SelectMapMarkers
        public List<MapMarkers> Get() 
        {
             using (mytestContext context = new mytestContext())
            {
                // Get the database from the context.
                var database = context.Database;
                 string sql = "select MerchantName, MerchantLongitude, MerchantLatitude, MerchantMiniUrl, Deal, CategoryDescription from mytest.tblMerchants m inner join mytest.tblMerchantDeals d on d.merchantId = m.merchantid inner join mytest.tblMerchantCategories mc on mc.merchantid = m.merchantid inner join mytest.tblCategories c on c.categoryid = mc.categoryid where m.MerchantActive = 1 and (d.dealstartdate <= getdate() and d.dealenddate >= getdate()) ";

                List<MapMarkers> result = database.SqlQuery<MapMarkers>(sql).ToList<MapMarkers>();
                return result;

            }
        }
        }
    }

1 个答案:

答案 0 :(得分:0)

这就是我想要的......

使用SSMS转到数据库并手动执行t-sql行并查看结果。