views:

191

answers:

6

Hello Everyone, I am trying to write a linq query that will only return certain columns from my entity object into a list object.

Below is my code which produces an error(can't implicitly convert a generic list of anonymous types to a generic list of type TBLPROMOTION):

IQueryable<TBLPROMOTION> matches = webStoreContext.TBLPROMOTION.Include("TBLSTORE").Include("LKPROMOTIONTYPE");

List<TBLPROMOTION> promotionInfo = null;

promotionInfo = (from p in matches
                orderby p.PROMOTION_NM descending
                select new { p.EFFECTIVE_DT, p.EXPIRE_DT, p.IS_ACTIVE, 
                p.PROMOTION_DESC, p.PROMOTION_ID, p.PROMOTION_NM }).ToList();

What would be the best way to accomplish this. I do not want to do a "select p" in this case and return all the columns associated with the query.

thanks in advance, Billy

A: 

did you try

 select new TBLPROMOTION {.....

instead of

 select new {.....
Fredou
This produces a syntax error saying "Can't initialize type TBLPROMOTION with a collection initializer because it does not implement type IEnumerable".
Billy Logan
+4  A: 

Can't you do var promotionInfo = () and get a list of anonymous types?

wRAR
In the end i had to go with the anonymous types. Thanks for your help.
Billy Logan
+1  A: 
List<TBLPROMOTION> promotionInfo = null;

promotionInfo = (from p in matches
                orderby p.PROMOTION_NM descending
                select new TBLPROMOTION { COL1 = p.EFFECTIVE_DT, COL2 = p.EXPIRE_DT, COL3 = p.IS_ACTIVE... }).ToList();

Where COL1, COL2, ... are the names of the properties on TBLPROMOTION you wish you populate.

Matt Dearing
That will fail at runtime in L2E; you can't instantiate entity types with initializers, only POCOs or anonymous types.
Craig Stuntz
Tried this and failed at runtime as craig just stated.
Billy Logan
I thought TBLPROMOTION was a POCO and matches contained the Entity Type. After the question's update I see that is not the case.
Matt Dearing
A: 

If you want a subset of the table you have 2 options:

  • @Fredou mentioned select new TBLPROMOTION{...}

  • other way is to create a custom DTO which has the exact properties & select them instead like:

    List promotionInfo = ... select new TBLPROMOTION_DTO{ Effective_dt = ... }

HTH

Sunny
+1  A: 
AllenG
Sorry, this would not compile for me. I get an error stating that TBLPROMOTION doesn't have a constructor that takes 6 arguments. Thanks.
Billy Logan
Did you try to instatiate the object first?
AllenG
going to go with the anonymous type solution. Had to give you a mark for all your help, but another person gave that answer long ago. Was just hoping i didn't have to go that route. Thank you for you help and time.
Billy Logan
+2  A: 

Okay, basically you can not cast an Anonymous type to a known type like TBLPROMOTION.

ofcourse, you can say var promotionInfo = and then get an IEnumerable<{Anonymoustype}> and use that to do, what you were wanting to do with promotionInfo.

Also, personally I prefer the Fluent version of a linq query, easy on the eyes, good programming diet, at least for me :)

var promotionInfo = matches
                .OrderByDescending( p => p.PROMOTION_NM)
                .Select( p => new { p.EFFECTIVE_DT, 
                                p.EXPIRE_DT, 
                                p.IS_ACTIVE, 
                                p.PROMOTION_DESC, 
                                p.PROMOTION_ID, 
                                p.PROMOTION_NM})
                .ToList();
Vin
'Fluent' targeted to EF also works faster
vittore
going to go with the anonymous type solution. I gave you a mark since this did help point me in the right direction. Thank you for your time.
Billy Logan
Sure, glad I could help...
Vin