Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous Type with Linq and Guid

I have a simple table:

ID | Value

When I do this:

var sequence = from c in valuesVault.GetTable()
               select new {RandomIDX = Guid.NewGuid(), c.ID, c.Value};

each element in the projection has the value of the same guid... How do I write this so that I get a different random guid value for each of my element in the projection?

Edit

To clarify on the issue. The GetTable() method simply calls this:

        return this.context.GetTable<T>();

where the this.contenxt is the DataContext of type T.

The itteration is done as it's always done, nothing fancy:

     foreach (var c in seq) 
     {
          Trace.WriteLine(c.RandomIDX + " " + c.Value);
     }

Output:

bf59c94e-119c-4eaf-a0d5-3bb91699b04d What is/was your mother's maiden name?
bf59c94e-119c-4eaf-a0d5-3bb91699b04d What was the last name of one of your high school English teachers?
bf59c94e-119c-4eaf-a0d5-3bb91699b04d In elementary school, what was your best friend's first and last name?

Edit 2 Using out the box linq2Sql Provider. I had built some generic wrappers around it but they do not alter the way IQuaryable or IEnumerable function in the code.

like image 446
dexter Avatar asked Mar 10 '11 21:03

dexter


People also ask

Why use anonymous types with LINQ?

The great thing about anonymous types used with LINQ is that you don’t have to think about casting the result type of your queries. This is especially useful when you work with types that have several properties. The compiler will replace the var keyword by a real type, which is always a reference type.

Why can’t we assign null in LINQ?

The reason it cannot be assigned null, is because there is no type which is bound no null. The great thing about anonymous types used with LINQ is that you don’t have to think about casting the result type of your queries.

How to create an IEnumerable of an anonymous type?

If you write a query that creates an object of an anonymous type in the select clause, the query returns an IEnumerable of the type. The following example shows creation of an object of an anonymous type that's initialized with two properties, Amount and Message. The type of each property is inferred by the compiler.

What are the rules for declaring an anonymous type?

The rules for declaring an anonymous type are few: the property must have a name and value (for example Age = 25) The reason it cannot be assigned null, is because there is no type which is bound no null. The great thing about anonymous types used with LINQ is that you don’t have to think about casting the result type of your queries.


1 Answers

What is underneath valuesVault.GetTable()?

You probably have a Linq provider such as Linq 2 SQL.

That means that valuesVault.GetTable() is of type IQueryable which in turn means that the entire query becomes an expression.

An expression is a query that is defined but not yet executed.

When sequence is being iterated over, the query is executed using the Linq provider and that Linq provider and one of the steps it has to perform is to execute this expression: Guid.NewGuid(). Most Linq providers cannot pass that expression to the underlying source (SQL Server wouldn't know what to do with it) so it gets executed once and the result of the execution returned with the rest of the result.

What you could do is to force the valuesVault.GetTable() expression to become a collection by calling the .ToList() or .ToArray() methods. This executes the expression and returns an IEnumerable which represents an in-memory collection.

When performing queries against an IEnumerable, the execution is not passed to the Linq provider but executed by the .NET runtime.

In your case this means that the expression Guid.NewGuid() can be executed correctly.

Try this:

var sequence = from c in valuesVault.GetTable().ToArray()
               select new {RandomIDX = Guid.NewGuid(), c.ID, c.Value};

Notice the .ToArray() there. That is what will make the statement go from IQueryable to IEnumerable and that will change its behaviour.

like image 74
Mikael Östberg Avatar answered Nov 04 '22 06:11

Mikael Östberg