Linq查询中的guide . tostring()

本文关键字:tostring guide 查询 Linq | 更新日期: 2023-09-27 18:11:14

我有一个方法,它试图从我的数据库拉一个记录与一个简单的where子句。我有一个问题,因为我正在传递一个字符串值并将其匹配到Guid (uniqueIdentifier)。我需要将传递的值作为字符串绑定到DataGrid中,并且需要该方法返回字符串。

显然在运行时,LinQ不能编译查询。.ToString()方法不能编译成SQL。什么好主意吗?

private string getFileLocation(string LinkGuid)
{
    try
    {
        ISESEntities context = new ISESEntities();
        string query = (from f in context.tbFileAttachments
                where f.CCCPGUID.ToString() == LinkGuid
                select f.FileLocation).First();
        return query;           
    }
    catch(Exception e)
    {
       blah blah
    }
}

Linq查询中的guide . tostring()

切换您的逻辑并将LinkGuid参数转换为Guid:

private string getFileLocation(string LinkGuid)
{
    try
    {
        Guid search = Guid.Parse(LinkGuid);
        ISESEntities context = new ISESEntities();
        string query = (from f in context.tbFileAttachments
                where f.CCCPGUID == search
                select f.FileLocation).First();
        return query;           
    }
    catch(Exception e)
    {
       blah blah
    }
}

而不是在Guid字段调用ToString,将您的参数字符串转换为Guid,然后在您的查询中传递:

Guid parameterGuid = Guid.Parse(LinkGuid);
string query = (from f in context.tbFileAttachments
               where f.CCCPGUID == parameterGuid 
               select f.FileLocation).First();

可能,LINQ提供程序不知道如何将ToStringGuid属性转换为SQL查询。我将使用Guid对象进行比较,例如:

Entities context = new ISESEntities();
Guid linkGuid = Guid.Parse(LinkGuid);
string query = (from f in context.tbFileAttachments
                where f.CCCPGUID == linkGuid
                select f.FileLocation).First();
return query;