Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

c# - Selecting a DTO object from an entity framework query where most properties are the same, is there a better way?

So, I have the following EF query:


private List<PolysubByYearDto> buildDtos(IQueryable<vw_Polysub> polysubs)
        {
            var query = from p in polysubs
                         join c in uowTr.CountyRepository.List() on p.primary_cty equals c.County_Code into lj1
                         from polysubWithCounty in lj1.DefaultIfEmpty()
                         join t in uowTr.PolysubRepository.ListPolyType() on p.poly_type_cd equals t.poly_type_cd into lj2
                         from polysubWithType in lj2.DefaultIfEmpty()
                         join period in uowTr.PolysubRepository.ListReportPeriods() on p.rec_no equals period.rec_no into lj3
                         from polysubWithPeriod in lj3.DefaultIfEmpty()
                        select new PolysubByYearDto()
                        {
                            PrimaryCountyName = (polysubWithCounty == null ? String.Empty : polysubWithCounty.county_name),
                            PolyTypeName = (polysubWithType == null ? String.Empty : polysubWithType.poly_name_ds),
                            ReportYearEndMonth = ((polysubWithPeriod == null || !polysubWithPeriod.year_end_month.HasValue) ? 0 : polysubWithPeriod.year_end_month.Value),
                            taxrate_year = p.taxrate_year
                            //taxrate_year and every property to follow (a lot) are all from the vw_Polysub object
                            //and share the same names as the PolysubByYearDto object properties...
                            // is there a better way to just make them all map without explicitly naming all properties?
                        };

            return query.ToList();
        }

The comment inside the "select" explains my problem. The PolysubByYearDto class is a child of vw_Polysub so it contains all of the same properties plus 3 more. Those 3 properties are set from joined data in the query, but every single property afterward just requires parent to child object property mapping. There are many properties and they all share the same names. Is there a way to do this without explicitly naming every property? The vw_Polysub object is generated from a view so it doesn't have reference properties which is why I had to join the data. I do not want to .toList() the query and then use reflection/automapper to map the properties because of performance reasons, I want the PolysubByYearDto objects to come out of the database. Thanks!

question from:https://stackoverflow.com/questions/65849242/selecting-a-dto-object-from-an-entity-framework-query-where-most-properties-are

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...