Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Tuesday, July 19, 2011

Case Insensitive Replace() function

For some reason .Net framework does not have the case insensitive replace method in the String class. I found it can very easily be implemented for String class using Extension Method feature of .Net.

Here's the method:
public static string Replace(this string originalString, string oldValue, string newValue, StringComparison comparisonType)
{
int startIndex = 0;
while (true)
{
startIndex = originalString.IndexOf(oldValue, startIndex, comparisonType);
if (startIndex == -1)
break;

originalString = originalString.Substring(0, startIndex) + newValue + originalString.Substring(startIndex + oldValue.Length);

startIndex += newValue.Length;
}

return originalString;
}


And calling this method seems trivial:
originalString = originalString.Replace(@"ABCD", "abcd", StringComparison.InvariantCultureIgnoreCase);


That's it.

Wednesday, September 1, 2010

Could not find default endpoint

This was a weird error I got while trying to consume a WCF service in my web application.
Could not find default endpoint element that references contract 'WS_User.IUser' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.


My project setup was so that the service reference was made in a class library whose method, user authentication, was invoked by the web application. So the solution to this problem was that I had to have the binding information at two places:
a.) The class library that was making the call to WCF service, and
b.) The web application that was making a call to the method in class library.

A bit weird behavior in my view, but that did away with the issue.

Friday, July 9, 2010

CopyToDataTable: LINQ to DataSet

Linq has a built in CopyToDataTable() method to convert resultset of a LINQ query to DataTable. This works fine if the query returns DataRow type. But surprisingly they don't work if a LINQ query implements a JOIN.
For example, the resultset of following query can not be converted into a DataTable using CopyToDataTable() method.
var final = from s in sourceTable.AsEnumerable()
join d in destinationTable.AsEnumerable() on s.Field("Id1") equals d.Field("Id2")
select new { Name = d.Field("Name"), Zip = s.Field("Zip"), Error = d.Field("Error") };


After a bit of toiling I found that to achieve this it will require implementation of an extension method. It was described here:
http://blogs.msdn.com/b/aconrad/archive/2007/09/07/science-project.aspx

And also on MSDN

I was using the CopyToDataTable() method over other queries too and implementation of the extension methods as suggested above resulted in exceptions where the method was called earlier.

I figured it will be safe if I rename the extension methods to avoid ambiguity. And that also helped the earlier exception to go away.