3

I want retrieve the data, and set property that was added to entityobject(via partial class) on fly.

How can I do that?

this is my query:

var ret = (from item in contex.Items
               select new Itemm
               {   
                  Title = item.Title,
                  Count = 1 // more complex: Count =item.refToAnotherEntity.Count()                                          
               });

public partial class Itemm 
{
     public int Count { get; set; }
}

I get error that: The entity or complex type cannot be constructed in a LINQ to Entities query

asker
  • 881
  • 2
  • 10
  • 15

4 Answers4

0

I'm trying to achieve the same result you are. My best solution up until now was to create a Wrapper to my entity that has the Count property to set the extra property on the partial class. Taking your case as an example this would be like:

public class ItemWithCount
{
   public Item Item { get; set; }
   public int Count
   {
        get { return Item.Count; }
        set { Item.Count = value; }
   }
}

Then you could make your query like this:

var ret = (from item in contex.Items
           select new ItemWithCount
           {   
              Item = item,
              Count = item.refToAnotherEntity.Count()                                          
           }).AsEnumerable().Select(x => x.Item);

Instead of creating the wrapper you could select an anonymous type with the same 2 properties, but this means that after the query you would have to go through the results and set the value of the property Count of each entity. Something like this:

var ret = (from item in contex.Items
           select new
           {   
              Item = item,
              Count = item.refToAnotherEntity.Count()                                          
           });
Parallel.ForEach(ret, x => x.Item.Count = x.Count);
var results = ret.Select(x => x.Item);

I preffer the first option, althouth I would like to have a solution without having to create a new class... Any ideas?

nflash
  • 430
  • 3
  • 16
0

The problem is that Linq to Entities is trying to convert the entire expression to SQL. That's not exactly possible with your "new" statement.

Try the following:

var ret = (from item in contex.Items
           select new Itemm
           ).ToList();
ret = (from item in ret
       select new Itemm
           {   
              Title = item.Title,
              Count = 1                                           
           }).ToList();

Basically you're making sure the results are returned from the SQL server (the ToList() does this) before trying to re-construct them. It's not pretty, and you can probably clean it up a bit, but the above code should work.

Timothy Baldridge
  • 10,455
  • 1
  • 44
  • 80
  • And how to be if I want to set Count with the value evluated from a query? For example number of Users that has this item? (when a reference to another entity exists) – asker Jan 24 '11 at 19:49
  • Try rephrasing the question as that ^^ makes no sense at all. – Timothy Baldridge Jan 24 '11 at 19:53
0

Your query will not translate to correct SQL as linq will not know what to do with your extra propery.

There are ways to initialize your custom properties.

You can create

partial void OnCreated() {}

And write your custom initialization here and if you Want to load after it loaded from database then you can Override

protected override void OnLoaded(bool initial) { base.OnLoaded(initial); }

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
-1

If you are willing to move the processing to the client side (i.e. move from IQueryable<T> to IEnumerable<T>) then you could do something like this:

var query = (from item in context.Items
    select new Item {                     
        Title = item.Title
    });

var ret = query.AsEnumerable().Select(i => {
    // Set the count.
    i.Count = 1;

    // Return the item.
    return i;
});

The key is in dropping down to IEnumerable<T>, at that point, the LINQ provider becomes LINQ to Objects which won't give you this problem.

casperOne
  • 73,706
  • 19
  • 184
  • 253