开发者

Search uniqueidentifier value in SQL Server 2008 table

I have a table in SQL Server 2008 with uniqueidentifier field.

In my application (C#, WPF) I have a textbox which intended for search some data by GUID, user can enter guid by typing, ctrl+v, etc, so he/she can make an error.

What is the best and fastest approach to make a search by this field using linq2sql ignoring whitespaces, "-"... any other characters?

var d = from status in dc.FolderStatus.Where(status => status.Date <= DateTime.Now 
                    && status.Folder.TapeCode.ToString().Replace("-", "") == tapeCode.ToUp开发者_运维知识库per().Replace("-", "").Replace(" ", "")
                    )

TapeCode is uniqueidentifier in SQL Server 2008 and Guid in dbml file. Is it ok?


No, that will be extremely slow (it'll have to do a table scan, convert each identifier to a string and compare the string). Instead, you'll have to convert the input string to a Guid and compare that:

tapeCode = Regex.Replace(tapeCode, "[^0-9a-zA-Z]+", "");
Guid id = Guid.Parse(tapeCode);
var d = from status in dc.FolderStatus
        where status.Date <= DateTime.Now 
           && status.Folder.TapeCode == id
        select status;

(Note that Guid.Parse method accepts 32 hexadecimal digits without dashes as a valid input).


If it's a uniqueidentifier field in SQL, I'd expect native support in LINQ to SQL. I believe you should be able to do:

Guid tapeCode = new Guid(text);
var d = from status in dc.FolderStatus
        where status.Date <= DateTime.Now && status.Folder.TapeCode == tapeCode
        select ...;

Now of course that will throw an exception if text doesn't represent a valid GUID. If you're using .NET 4, you can use Guid.TryParse in the same way you would for parsing integers etc:

Guid tapeCode;
if (Guid.TryParse(text, out tapeCode))
{
    var d = from status in dc.FolderStatus
            where status.Date <= DateTime.Now &&
                  status.Folder.TapeCode == tapeCode
            select ...;
}
else
{
    // Handle invalid input
}


You shouldn't try and do .Replace on the SQL field, as this will actually get executed on the server, which is very slow.

You should massage the data coming in to your method.

I.e

public void GetFoo(string guid)
{
     string sqlGuid = guid.ToString().Trim().Replace("-", "");

     var d = from status in dc.FolderStatus.Where(status => status.Date <= DateTime.Now &&
                                                            status.Folder.TapeCode == sqlGuid);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜