Query returning wrong value?

后端 未结 1 1632
滥情空心
滥情空心 2021-01-26 18:40

Thanks to the help Ive recieved from zambonee on write a query that applies to an entire db instead of a table

Using EF i\'m writing a query that returns the userId from

相关标签:
1条回答
  • 2021-01-26 19:14

    You may have some misunderstanding about ExecuteStoreQuery<T>, it will return the Type you specified. In your case, it will return string back.

    Guid aspUserIdToRemove = Context.ExecuteStoreQuery<string>("Select UserId FROM aspnet_Users where UserName LIKE '%" + userName + "%'").ElementType.GUID;
    

    With this statement, ExecuteStoreQuery<string> will return a string type of UserId, and then get the GUID from ElementType, but not GUID of Users

    To solve that, you just need to use

    string aspUserIdToRemove = Context.ExecuteStoreQuery<string>("Select UserId FROM aspnet_Users where UserName LIKE '%" + userName + "%'");
    

    More better you may want to avoid SQL injection, and use parameter

    string aspUserIdToRemove = Context.ExecuteStoreQuery<string>("Select UserId FROM aspnet_Users where UserName LIKE '%{0}%'", userName);
    

    Details you can check the API

    As the aspUserIdToRemove is a string, you don't need to use .ToString() on it. However, I don't have enough data, you may need to check do you need to escape the '{}'.

    Moreover, from your comment, the \ is an escape character, if you want to concat in a string, you need to escape that with \\ (API)

    0 讨论(0)
提交回复
热议问题