14

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?

0

3 Answers 3

3

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
    };
Sign up to request clarification or add additional context in comments.

5 Comments

But then you're not restricting the query to the fields specified, which is one of the key points of doing the select to begin with.
If you were concerned about not pulling extraneous data from the DB, you could create an anonymous class in between. I don't understand why he's using structs.
I think he's more interested in 'why', then 'how do I fix'.
It would seem he's trying to avoid the overhead of creating all of those class instances and to instead use a struct. As he said, using a class (it wouldn't matter if it's anonymous or not) works fine, but a struct doesn't.
@Shlomo yes I posted the question to understand why it doesn't work with struct. I fixed it using a class and yes I need my custom defined type because the query will be returned by a method using my custom type as the return type: IList<MyStruct>
2

Linq to entities does not support projecting into structs. They would have needed to design support for this into the query provider, and they simply choose not to. It would appear that they didn't see it as a valuable enough feature to support for it to be worth the development cost.

You will need to project into new class instances in your queries.

1 Comment

> Linq to entities does not support projecting into structs < How did you get to that conclusion? Did you read it somewhere? I'm not questioning your knowledge I just wanna know if you used some mechanism to find it out so I can do the same in the future.
-1

Try this:

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

Products[] p1 = new Products[] { new Products { name = "prod1", amount = 5 }
var c = from p in p1
        select new MyStruct { name = p.name, amount = p.amount };

1 Comment

He's not doing Linq to objects, he's using a query provider, and it's the query provider that's having trouble interpreting this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.