473,379 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. 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);
  2.  
  3. foreach (ContactResult r in results)    <<<< Error occurs here
  4. {
  5. ...
  6. }
  7.  
Method 2: Strongly Typed to custom class ContactResult
Expand|Select|Wrap|Line Numbers
  1. public class ContactResult
  2. {
  3.         public Guid ID;
  4.         public String Title;
  5.         public String LastName;
  6.         public String FirstName;
  7.         public String Email;
  8.  }
  9.  
Expand|Select|Wrap|Line Numbers
  1. 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);
  2.  
  3. foreach (ContactResult r in results)    <<<< Error occurs here
  4. {
  5. ...
  6. }
  7.  
Both methods encounter this error - which results due to the GUID

Expand|Select|Wrap|Line Numbers
  1. [InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.]
  2.    System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +7569852
  3.    System.String.System.IConvertible.ToType(Type type, IFormatProvider provider) +8
  4.    System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +7601872
  5.    Mindscape.LightSpeed.Linq.Utils.TypeHelper.NullableSafeConvert(Object value, Type targetType) +245
  6.    Mindscape.LightSpeed.Linq.ProjectedTypeBuilder.GetValue(Int32 expressionIndex, IDataRecord record, Type targetType) +173
  7.    Mindscape.LightSpeed.Linq.NamedTypeBuilder.InitialiseMembersFromReader(MemberInitExpression memberInit, IDataRecord record, Object instance) +476
  8.    Mindscape.LightSpeed.Linq.NamedTypeBuilder.Build(IDataRecord record) +103
  9.    Mindscape.LightSpeed.Linq.Plan.SingleQueryPlan.ProjectNative(IUnitOfWork unitOfWork) +223
  10.    Mindscape.LightSpeed.Linq.Plan.SingleQueryPlan.ExecuteImmediate(IUnitOfWork unitOfWork, Type returnType) +1580
  11.    Mindscape.LightSpeed.Linq.LinqQueryProvider.Execute(Expression expression) +237
  12.    Mindscape.LightSpeed.Linq.LinqQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +37
  13.    Mindscape.LightSpeed.Linq.LinqQuery`1.GetEnumerator() +40
  14.  
Any help would be greatly appreciated in trying to eliminate this error, I need to be able to get the GUID.

Thanks,
Scott
Mar 24 '10 #1

✓ answered by scott1010

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

2 6373
Christian Binder
218 Expert 100+
I think a error clearly states what's happening ...
[InvalidCastException: 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
Expand|Select|Wrap|Line Numbers
  1. Guid g = new Guid(guidStirng);
Applied to your example:
Expand|Select|Wrap|Line Numbers
  1. var results = (from c in uow.Contacts 
  2.                orderby c.LastName ascending, c.FirstName ascending 
  3.                select new ContactResult(){ID=new Guid(c.Id), Title = c.Title, LastName=c.LastName, FirstName=c.FirstName, Email=c.ContactEmailAddresses[0].Email }
  4.               ).Take(2);
Mar 25 '10 #2
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
Mar 25 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
0
by: John Smith | last post by:
Hello people, I have a performance query regarding LINQ that I would like some opinions. Currently we have a business logic framework that is used in n-tier applications. We read data from a...
0
by: Jake McCool | last post by:
Hello people, I have a performance query regarding LINQ that I would like some opinions. Currently we have a business logic framework that is used in n-tier applications. We read data from a...
15
by: shapper | last post by:
Hello, I have two Lists: A = {ID, Name} = { (Null, John), (Null, Mary), (Null, Andrew), (Null, Peter) } B = {ID, Name} = { (1, John), (2, Robert), (3, Angela), (4, Andrew) } I want to...
2
by: Nightcrawler | last post by:
I have the following tables that I have dragged into a .dbml file ( have only included the keys for simplicity). Users have a one to many relationship on Items keyed on UserId = UserId. Users...
3
by: Vivien Parlat | last post by:
Hello, I am currently using VB.Net 2008 express. I use linq to perform queries on a database, and I'm using the following link's source to convert those queries into DataTables i can then bind...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
2
by: Neil Chambers | last post by:
All, I have a class describing various actions to take against a LINQ to SQL datasource. What are the pros/cons of instantiating the LINQ object either in the root of the class (for lack of a...
1
by: shapper | last post by:
Hello, I am parsing a CSV string as follows: var answers = CSVAnswers.Split(new char { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(a =a.Trim()).ToList(); I would like to create a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.