Hello I am having problems with a Linq query. I need to return an ID field which is of type GUID (c.Id) along with other fields of type String.
I have tried both anonymous types and strongly typed objects but continue to encounter the same error:
Method 1: Anonymously Typed
Method 2: Strongly Typed to custom class ContactResult
Both methods encounter this error - which results due to the GUID
Any help would be greatly appreciated in trying to eliminate this error, I need to be able to get the GUID.
Thanks,
Scott
I have tried both anonymous types and strongly typed objects but continue to encounter the same error:
Method 1: Anonymously Typed
Code:
var results = (from c in uow.Contacts orderby c.LastName ascending, c.FirstName ascending select new {ID=c.Id, Title = c.Title, LastName=c.LastName, FirstName=c.FirstName, Email=c.ContactEmailAddresses[0].Email }).Take(2);
foreach (ContactResult r in results) <<<< Error occurs here
{
...
}
Code:
public class ContactResult
{
public Guid ID;
public String Title;
public String LastName;
public String FirstName;
public String Email;
}
Code:
var results = (from c in uow.Contacts orderby c.LastName ascending, c.FirstName ascending select new ContactResult(){ID=c.Id, Title = c.Title, LastName=c.LastName, FirstName=c.FirstName, Email=c.ContactEmailAddresses[0].Email }).Take(2);
foreach (ContactResult r in results) <<<< Error occurs here
{
...
}
Code:
[InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.] System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +7569852 System.String.System.IConvertible.ToType(Type type, IFormatProvider provider) +8 System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +7601872 Mindscape.LightSpeed.Linq.Utils.TypeHelper.NullableSafeConvert(Object value, Type targetType) +245 Mindscape.LightSpeed.Linq.ProjectedTypeBuilder.GetValue(Int32 expressionIndex, IDataRecord record, Type targetType) +173 Mindscape.LightSpeed.Linq.NamedTypeBuilder.InitialiseMembersFromReader(MemberInitExpression memberInit, IDataRecord record, Object instance) +476 Mindscape.LightSpeed.Linq.NamedTypeBuilder.Build(IDataRecord record) +103 Mindscape.LightSpeed.Linq.Plan.SingleQueryPlan.ProjectNative(IUnitOfWork unitOfWork) +223 Mindscape.LightSpeed.Linq.Plan.SingleQueryPlan.ExecuteImmediate(IUnitOfWork unitOfWork, Type returnType) +1580 Mindscape.LightSpeed.Linq.LinqQueryProvider.Execute(Expression expression) +237 Mindscape.LightSpeed.Linq.LinqQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +37 Mindscape.LightSpeed.Linq.LinqQuery`1.GetEnumerator() +40
Thanks,
Scott
Comment