将GUID转换为字符串实体框架

时间:2014-06-12 19:41:33

标签: c# linq entity-framework asp.net-mvc-3 linq-to-sql

冒着提出重复问题的风险,我似乎无法让它发挥作用。有很多我想做的例子,但语法并不完全相同,没有任何效果。我正在教自己Linq。此查询从SQL Server获取ID列表。没有" ToString()"它说"无法将GUID转换为string []"。请提前通知并表示感谢。

    public string[] GetAllRoleIDs(string param)
    {           
        using (DBEntities de = new DBEntities())
        {
            string[] roleset = (from p in de.MySQLView
                         where p.anotherfield == param
                                select p.RoleID.ToString()).Distinct().ToArray();

            return roleset;
        }            
    }

1 个答案:

答案 0 :(得分:0)

您希望AsEnumerable()强制ToString()调用本地,而不是作为EF查询的一部分。例如:

using (DBEntities de = new DBEntities())
{
    string[] roleset = de.MySQLView
                         .Where(p => p.anotherfield == param)
                         .Select(p => p.RoleID)
                         .Distinct()
                         .AsEnumerable()
                         .Select(guid => guid.ToString())
                         .ToArray();
    return roleset;
}

(我已经将它从查询表达式转换为使用点符号,因为两者之间的混合和匹配很麻烦,而且你只有whereselect。你可以如果你愿意,可以使用查询表达式。)

相关问题