开发者

Linq - How to query specific columns and return a lists

开发者 https://www.devze.com 2022-12-25 20:04 出处:网络
I am trying to write a linq query that will only return certain columns from my entity object into a list object.

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>开发者_JAVA技巧; 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


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


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();


If you're moving from a L2E query to a Type already defined, you may need a step between. I haven't tried to compile this but something like:


   List<TBLPROMOTION> promotions = new List<TBLPROMOTION>();

var results = 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 };

   foreach (var v in results)
   {
       promotions.Add(new TBLPROMOTION(v.EFFECTIVE_DT, v.EXPIRE_DT, v.IS_ACTIVE, 
       v.PROMOTION_DESC, v.PROMOTION_ID, v.PROMOTION_NM));
   }

Based on the comment below, you might try something like:


    foreach(var v in results)
    {
        TBLPROMOTION temp = new TBLPROMOTION();
        temp.EFFECTIVE_DT = v.EFFECTIVE_DT;
        temp.EXPIRE_DT = v.EXPIRE_DT;
        temp.IS_ACTIVE = v.IS_ACTIVE
        // Assign Other Properties

    promotions.Add(temp);
}

....... Sorry: Just read the addition to the top.

Are you sure that none of the fields you're leaving out (instead of saying "select p") are required for a TBLPROMOTION object? Also, sense your TBLPROMOTION object is going to have properties (and therefore memory allocated) for those skipped fields, why not just use an annonymous type or set up a helper class that contains only your needed properties?


@Billy, following code worked for me.

List<TBLPROMOTION> promotionInfo = 
                      (from p in matches
                       orderby p.PROMOTION_NM descending
                       select new TBLPROMOTION(p.EFFECTIVE_DT, p.EXPIRE_DT, p.IS_ACTIVE, 
                                       p.PROMOTION_DESC, p.PROMOTION_ID, p.PROMOTION_NM)
                      ).ToList();


did you try

 select new TBLPROMOTION {.....

instead of

 select new {.....


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消