Linq Query returning GUID

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scott1010
    New Member
    • Jul 2009
    • 6

    Linq Query returning GUID

    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
    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
    {
    ...
    }
    Method 2: Strongly Typed to custom class ContactResult
    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
    {
    ...
    }
    Both methods encounter this error - which results due to the GUID

    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
    Any help would be greatly appreciated in trying to eliminate this error, I need to be able to get the GUID.

    Thanks,
    Scott
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    I think a error clearly states what's happening ...
    [InvalidCastExce ption: Invalid cast from 'System.String' to 'System.Guid'.]
    So c.Id is probably a String, not a Guid!
    But you can make a Guid out of a String with
    Code:
    Guid g = new Guid(guidStirng);
    Applied to your example:
    Code:
    var results = (from c in uow.Contacts 
                   orderby c.LastName ascending, c.FirstName ascending 
                   select new ContactResult(){ID=new Guid(c.Id), Title = c.Title, LastName=c.LastName, FirstName=c.FirstName, Email=c.ContactEmailAddresses[0].Email }
                  ).Take(2);

    Comment

    • scott1010
      New Member
      • Jul 2009
      • 6

      #3
      No its definitely a GUID.

      I had a further thought after I posted and it turns out it was bug in database tool I was using, and they are releasing a fix.

      Thanks
      Scott

      Comment

      Working...