context.Database.SqlQuery不返回我声明的对象列表

时间:2015-08-11 13:34:25

标签: c# entity-framework

我有一些调用GetSQLUser的代码(在下面的代码中定义)...如果我在var result上放置一个断点,结果就不像我预期的那样(一个SqlUserInfo对象......会很好。)

public class Whatever
{
    public void GetSQLUser()
    {
        var result = this.context.Database.SqlQuery<SQLUserInfo>("SELECT SYSTEM_USER AS UserID");
    }
}

public class SQLUserInfo
{
    string UserID { get; set; }
}

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试更改:

var result = this.context.Database.SqlQuery<SQLUserInfo>("SELECT SYSTEM_USER AS UserID").First();

<#-- this variable we need to store unique author ids -->
<#assign authorids = [] />
<#-- I'd use API v2 here, think for such stuff it's more powerful than v1 -->
<#assign query = 'SELECT author FROM messages WHERE conversation.style = "blog" AND board.id = "audiofiles"'?url />
<#assign response = rest("2.0", "/search?q=" + query) />

<#-- the response object will contain a list of blog post authors,
     we loop trough them to get uniqe ids of every user that has written
     a blog post, we need them later -->
<#list response.data.items as author>
    <#-- make sure each author just gets added once -->
    <#if !authorids?seq_contains(author.id)>
        <#assign authorids = authorids + [author.id] />
    </#if>
</#list>

<#-- now we loop trough the unique author ids and ask for the amount of
     blog posts they have written -->
<ul>
<#list authorids as id>
    <#assign query = 'SELECT count(*) FROM messages WHERE author.id = id AND board.id = "audiofiles"'?url />
    <#assign response = rest("2.0", "/search?q=" + query) />

    <li>User with ID ${id} has written ${response.data.count} blog posts</li>
</#list>
</ul>