1+ // ***********************************************************************
2+ // Assembly : RzR.Shared.Extensions.DomainCommonExtensions
3+ // Author : RzR
4+ // Created On : 2026-01-19 22:01
5+ //
6+ // Last Modified By : RzR
7+ // Last Modified On : 2026-01-19 22:42
8+ // ***********************************************************************
9+ // <copyright file="TypeParamEntityExtensions.cs" company="RzR SOFT & TECH">
10+ // Copyright © RzR. All rights reserved.
11+ // </copyright>
12+ //
13+ // <summary>
14+ // </summary>
15+ // ***********************************************************************
16+
17+ #region U S A G E S
18+
19+ using System . Collections . Generic ;
20+ using System . Linq ;
21+ using DomainCommonExtensions . ArraysExtensions ;
22+ using DomainCommonExtensions . Utilities . Ensure ;
23+
24+ #endregion
25+
26+ namespace DomainCommonExtensions . CommonExtensions . TypeParam
27+ {
28+ /// -------------------------------------------------------------------------------------------------
29+ /// <content>
30+ /// T type extensions.
31+ /// </content>
32+ /// =================================================================================================
33+ public static partial class TExtensions
34+ {
35+ /// -------------------------------------------------------------------------------------------------
36+ /// <summary>
37+ /// Gets the property values in this collection.
38+ /// </summary>
39+ /// <typeparam name="TPropResultType">Type of the property result type.</typeparam>
40+ /// <typeparam name="TSourceEntity">Type of the source entity.</typeparam>
41+ /// <param name="source">The source to act on.</param>
42+ /// <param name="fieldNames">A variable-length parameters list containing field names.</param>
43+ /// <returns>
44+ /// An enumerator that allows foreach to be used to process the property values in this
45+ /// collection.
46+ /// </returns>
47+ /// =================================================================================================
48+ public static IEnumerable < TPropResultType > GetPropertyValue < TPropResultType , TSourceEntity > ( this TSourceEntity source ,
49+ params string [ ] fieldNames )
50+ where TPropResultType : struct
51+ where TSourceEntity : class
52+ {
53+ DomainEnsure . IsNotNull ( source , nameof ( source ) ) ;
54+
55+ var resultList = new List < TPropResultType > ( ) ;
56+ var props = source ! . GetType ( ) . GetProperties ( ) . Where ( x => fieldNames . Contains ( x . Name ) ) ;
57+ foreach ( var prop in props . NotNull ( ) )
58+ {
59+ var value = prop . GetValue ( source , null ) ;
60+ if ( value . IsNotNull ( ) )
61+ resultList . Add ( ( TPropResultType ) value ! ) ;
62+ }
63+
64+ return resultList ;
65+ }
66+
67+ /// -------------------------------------------------------------------------------------------------
68+ /// <summary>
69+ /// Gets the property string values in this collection.
70+ /// </summary>
71+ /// <typeparam name="TSourceEntity">Type of the source entity.</typeparam>
72+ /// <param name="source">The source to act on.</param>
73+ /// <param name="fieldNames">A variable-length parameters list containing field names.</param>
74+ /// <returns>
75+ /// An enumerator that allows foreach to be used to process the property string values in
76+ /// this collection.
77+ /// </returns>
78+ /// =================================================================================================
79+ public static IEnumerable < string > GetPropertyStringValue < TSourceEntity > ( this TSourceEntity source ,
80+ params string [ ] fieldNames )
81+ where TSourceEntity : class
82+ {
83+ DomainEnsure . IsNotNull ( source , nameof ( source ) ) ;
84+
85+ var resultList = new List < string > ( ) ;
86+ var props = source ! . GetType ( ) . GetProperties ( ) . Where ( x => fieldNames . Contains ( x . Name ) ) ;
87+ foreach ( var prop in props . NotNull ( ) )
88+ {
89+ var value = prop . GetValue ( source , null ) ;
90+ if ( value . IsNotNull ( ) )
91+ resultList . Add ( value ! . ToString ( ) ! ) ;
92+ }
93+
94+ return resultList ;
95+ }
96+
97+ /// -------------------------------------------------------------------------------------------------
98+ /// <summary>
99+ /// Gets the property values in this collection.
100+ /// </summary>
101+ /// <typeparam name="TSourceEntity">Type of the source entity.</typeparam>
102+ /// <param name="source">The source to act on.</param>
103+ /// <param name="fieldNames">A variable-length parameters list containing field names.</param>
104+ /// <returns>
105+ /// An enumerator that allows foreach to be used to process the property values in this
106+ /// collection.
107+ /// </returns>
108+ /// =================================================================================================
109+ public static IEnumerable < object > GetPropertyValue < TSourceEntity > ( this TSourceEntity source ,
110+ params string [ ] fieldNames )
111+ where TSourceEntity : class
112+ {
113+ DomainEnsure . IsNotNull ( source , nameof ( source ) ) ;
114+
115+ var resultList = new List < object > ( ) ;
116+ var props = source ! . GetType ( ) . GetProperties ( ) . Where ( x => fieldNames . Contains ( x . Name ) ) ;
117+ foreach ( var prop in props . NotNull ( ) )
118+ {
119+ var value = prop . GetValue ( source , null ) ;
120+ if ( value . IsNotNull ( ) )
121+ resultList . Add ( value ! ) ;
122+ }
123+
124+ return resultList ;
125+ }
126+
127+ /// -------------------------------------------------------------------------------------------------
128+ /// <summary>
129+ /// A TEntityResult extension method that change property value.
130+ /// </summary>
131+ /// <typeparam name="TEntityResult">Type of the entity result.</typeparam>
132+ /// <typeparam name="TEntitySource">Type of the entity source.</typeparam>
133+ /// <param name="sourceResultEntry">The sourceResultEntry to act on.</param>
134+ /// <param name="sourceDataEntry">Source data entry.</param>
135+ /// <param name="propsName">A variable-length parameters list containing properties name.</param>
136+ /// <returns>
137+ /// A TEntityResult.
138+ /// </returns>
139+ /// =================================================================================================
140+ public static TEntityResult ChangePropertyValue < TEntityResult , TEntitySource > (
141+ this TEntityResult sourceResultEntry ,
142+ TEntitySource sourceDataEntry , params string [ ] propsName )
143+ where TEntitySource : class
144+ where TEntityResult : class
145+ {
146+ DomainEnsure . IsNotNull ( sourceResultEntry , nameof ( sourceResultEntry ) ) ;
147+ DomainEnsure . IsNotNull ( sourceDataEntry , nameof ( sourceDataEntry ) ) ;
148+
149+ var props = sourceResultEntry ! . GetType ( ) . GetProperties ( ) . Where ( x => propsName . Contains ( x . Name ) ) ;
150+ foreach ( var prop in props . NotNull ( ) )
151+ {
152+ var sourceEntryProp = sourceDataEntry . GetType ( ) . GetProperty ( prop . Name ) ;
153+ if ( sourceEntryProp . IsNotNull ( ) )
154+ {
155+ var value = sourceEntryProp ! . GetValue ( sourceDataEntry , null ) ;
156+ prop ! . SetValue ( sourceResultEntry , value , null ) ;
157+ }
158+ }
159+
160+ return sourceResultEntry ;
161+ }
162+ }
163+ }
0 commit comments