Thursday, September 29, 2011

LINQ Query on SPListItem returning "Value does not fall within the expected range."

If you are doing LINQ Qery on SHarepoint SpListItem object and getting the following error: "Value does not fall within the expected range."
Might be this little simple (addition of .ToString()) will help you to make it work.
Original Code
List items = (from li in oList.Items.OfType()
where li["UserName"] == value
select li).ToList();

change this query to like this by adding .ToString() in li["ColName"]=="Value"
Changed Code
List items = (from li in oList.Items.OfType()
where li["Username"].ToString() == value
select li).ToList();

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.

No comments:

Post a Comment