Error creating a Linq query

.net, c#, entity-framework, linq, linq-to-objects

Solution

It works with LinqToObjects. I'm guessing LinqToEntities doesn't know how to create a struct. If you do this you'll be fine:

struct MyStruct
{
  public string name;
  public double amount;
}

var a = Products.AsEnumerable()
    .Select(p => new MyStruct
    {
        name = p.Name,
        amount = p.Amount
    };

Problem

I've a query like this one ``` struct MyStruct { public string name; public double amount; } var a = from p in Products select new MyStruct { name = p.Name, amount = p.Amount }; ``` When I execute the query I get the following exception: System.NotSupportedException {"Only parameterless constructors and initializers are supported in LINQ to Entities."} but if I change the type of MyStruct to class then it works as expected. Why it works with class and fail with struct?

Original source