I am trying to group by a partial zip code and if any are zip codes that are null or less than 3 characters group them as ""
I've seen some example of using a nullable comparer but not sure of how to fit something like that in the syntax in the context of below.
Also the QBModel.ResultsTable is a dynamic list and the CallerZipCode is a char(10) so something with the valid value, could be "96701-----"
var newset = (from rst in QBModel.ResultsTable
group rst by rst.CallerZipCode.Substring(0, 3) into newGroup
select new DataSourceRecord()
{
State = ToTitleCase(newGroup.Select(i => i.CallerState).FirstOrDefault()),
ZipCode = newGroup.Where(z => z.CallerZipCode.StartsWith(newGroup.Key)).Select(x => x.CallerZipCode.Substring(0, 3)).FirstOrDefault()
}).ToList();
Here is a nullable comparer I found but probably needs work if I'm going to check for zip codes less than 2 characters:
public class NullableComparer<T> : IEqualityComparer<T?> where T : struct
{
public bool Equals(T? x, T? y)
{
if (x == null || y == null)
return false;
return x.Equals(y);
}
public int GetHashCode(T? obj)
{
return obj.GetHashCode();
}
}
How can I change this code to accomplish what I am after?
[Edit]
Just tried something like this, but it didn't seem to work very well
var newset = (from rst in QBModel.ResultsTable
group rst by rst.CallerZipCode == null || rst.CallerZipCode.Trim().Length() < 3 ? "<null>" : rst.CallerZipCode.Substring(0, 3) into newGroup
select new DataSourceRecord()
{
State = ToTitleCase(newGroup.Select(i => i.CallerState).FirstOrDefault()),
ZipCode = newGroup.Where(z => z.CallerZipCode.StartsWith(newGroup.Key)).Select(x => x.CallerZipCode.Substring(0, 3)).FirstOrDefault()
}).ToList();