Might be this little simple (addition of .ToString()) will help you to make it work.
where li["UserName"] == value
change this query to like this by adding .ToString() in li["ColName"]=="Value"
where li["Username"].ToString() == value
If you like this solution, then care for a tree which needs some water, so water a plant and make that green.
Start using http://www.code4green.com as your free code generation tool for Sharepoint, PowerShell, C#, ASP.net, SQL, and many more languages.
let other know about this tool, because this tool is helping community to plant tree.