Address Table UserTable,此查询在SQL Server Management Studio中运行良好。 但是,当我尝试在C#中运行此查询时,它给出了异常,请帮助我。
我尝试了很多事情,但无法解决此问题。
SQL查询
set @LATITUDE=12
set @LONGITUDE=12
Select * FROM [ChefODine].[dbo].[User] Inner Join [ChefODine].[dbo].[Address] on [ChefODine].[dbo].[User].AID=Address.ID
WHERE AID IN (
SELECT Top 5 ID
FROM [ChefODine].[dbo].[Address]
ORDER BY (ABS(ABS(LAT)-ABS(@LATITUDE)))+ABS(ABS(Lng)-ABS(@LONGITUDE)))
C#代码
public HttpResponseMessage getNearByChef(double lat, double lng)
{
var user = db.Users.SqlQuery("Select * FROM [ChefODine].[dbo].[User] Inner Join [ChefODine].[dbo].[Address] on [ChefODine].[dbo].[User].AID=Address.ID WHERE AID IN( SELECT Top 5 ID FROM[ChefODine].[dbo].[Address] ORDER BY(ABS(ABS(LAT) - ABS("+lat+"))) + ABS(ABS(Lng) - ABS("+lng+"))) ");
return Request.CreateResponse(HttpStatusCode.OK,user);
}
这里是例外:
“消息”:“发生错误。”, “ ExceptionMessage”:“'ObjectContent'1'类型未能序列化内容类型'application / json; charset = utf-8'的响应主体。”, “ ExceptionType”:“ System.InvalidOperationException”, “ StackTrace”:null, “ InnerException”:{ “ Message”:“发生错误。”, “ ExceptionMessage”:“数据读取器与指定的'ChefODineModel.User'不兼容。类型为'Date_time'的成员在数据读取器中没有具有相同名称的对应列。 “ ExceptionType”:“ System.Data.Entity.Core.EntityCommandExecutionException”, “ StackTrace”:“位于System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.GetMemberOrdinalFromReader(DbDataReader storeDataReader,EdmMember成员,EdmType currentType,Dictionary'2 namedList)\ r \ n在System.Data.Entity.Core.Query .InternalTrees.ColumnMapFactory.GetColumnMapsForType(DbDataReader storeDataReader,EdmType edmType,Dictionary'2 namedList)\ r \ n位于System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndType(DbDataReader storeDataReader,EdmType edmType,EntitySet实体集2系统上的System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate [TElement](DbDataReader阅读器,字符串entitySetName,MergeOption mergeOption,布尔流,EntitySet&entitySet,TypeUsage&edmType)的rName Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal [TElement](字符串commandText,字符串entitySetName,ExecutionOptions执行选项,Object []参数)\ r \ n位于System.Data.Entity.Core.Obje cts.ObjectContext。<> c__DisplayClass65'1.b__64()\ r \ n位于System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction [T](Func'1函数,IDbExecutionStrategy执行策略,布尔值startLocalTransaction,布尔值releaseConnectionOnSuccess)\ System.Data.Entity.Core.Objects.ObjectContext上的r \ n。<> c__DisplayClass65'1.b__63()\ System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [rResult](Func'1操作) \ r \ n在System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably [TElement](字符串commandText,字符串entitySetName,ExecutionOptions执行选项,Object []参数)\ r \ n在System.Data.Entity.Core.Objects .ObjectContext.ExecuteStoreQuery [TElement](字符串commandText,字符串entitySetName,ExecutionOptions执行选项,Object []参数)\ r \ n位于System.Data.Entity.Internal.Linq.InternalSet'1。<> c__DisplayClass11.b__10()\ r \ n在System.Data.Entity.Internal.LazyEnumerator'1.MoveNext()\ r \ n在Newtonsoft.Json.Serialization.JsonSeria lizerInternalWriter.SerializeList(JsonWriter writer,IEnumerable values,JsonArrayContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Coner,Con价值在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value,Type objectType)处的容器合同,JsonProperty containerProperty)\ r \ n在Newton.Json.JsonSerializer.SerializeInternal(JsonWriter )\ r \ n位于System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型类型,对象值,流writeStream,编码有效编码)\ r \ n在System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(类型类型, System.Net.Http.F上的对象值,流writeStream,编码有效Encoding)\ r \ n Ormatting.BaseJsonMediaTypeFormatter.WriteToStream(类型类型,对象值,流writeStream,HttpContent内容)\ r \ n位于System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型类型,对象值,流writeStream,HttpContent内容,TransportContext transportContext, CancellationToken cancellingToken)\ r \ n ---从上一个引发异常的位置开始的堆栈结束跟踪--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime .CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n位于System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()“
答案 0 :(得分:0)
”数据读取器与指定的 “ ChefODineModel.User”。 “ Date_time”类型的成员不 在数据读取器中有一个具有相同名称的对应列。“,
不能用ChefODineModel.User映射用户对象,因为Dto模型中不存在date_time。 除此之外,当您必须将数据传递到查询中时,可以使用SqlParameter
public HttpResponseMessage getNearByChef(double lat, double lng)
{
string query = @"Select u.* FROM [ChefODine].[dbo].[User] u
Inner Join [ChefODine].[dbo].[Address] on [ChefODine].[dbo].[User].AID=Address.ID
WHERE AID IN( SELECT Top 5 ID FROM[ChefODine].[dbo].[Address] ORDER BY(ABS(ABS(LAT) - ABS(@LATITUDE))) + ABS(ABS(LATITUDE) - ABS(@LONGITUDE)))";
var user = db.Users.SqlQuery(query, new SqlParameter("@LATITUDE", lat), new SqlParameter("@LONGITUDE", lng)).ToList();
return Request.CreateResponse(HttpStatusCode.OK, user);
}