根据列属性值获取属性值

发布于 2024-12-28 20:12:22 字数 1386 浏览 1 评论 0原文

List<MyModel1> myModel1 = new List<MyModel1>();                    
MyUserModel myUserModel =  new MyUserModel();                    
List<MyModel2> myModel2 = new List<MyModel1>();
myModel1 = m_Service1.GetMyModelFields();
myUserModel = m_Service2.GetMyUserDetails();
myModel2 = (from myModel1Field in myModel1                        
             select new MyModel2 { FieldCaption = myModel1Field.FieldAlias, 
             FieldValue = "" }).ToList<MyModel2>();

myModel1Field.FieldAlias 文本将与 myUserModel 中的属性之一的 Column 属性之一的值相同。因此,我必须在 myUserModel 中搜索列 atribute(Name) 并获取相应的属性值并将其分配给“FieldValue”。如果我在 myUserModel 中找不到该值,我可以将“FieldValue”设置为“NA”

当我知道属性名称时,获取属性的列属性(名称)值的一种方法如下。

myUserModel.GetType().GetProperty("FirstName").GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute), false).Cast<System.Data.Linq.Mapping.ColumnAttribute>().Single().Name

但就我而言,属性名称将不为人所知。我必须根据 myModel1Field.FieldAlias 值找到该属性。这件事该怎么办呢。请建议。

MyUserModel 及其其中一个属性

public class MyUserModel { 
[Column(Name = "first_name", DbType = "varchar")] 
public string FirstName { get; set; } 
}

现在,如果 myModel1Field.FieldAlias 是“first_name”,那么我必须在 MyUserModel 中搜索 Column 属性(名称)作为 first_name 的属性。如果它存在,我必须将其值设置为“FieldValue”。否则将“FieldValue”设置为“NA”。

List<MyModel1> myModel1 = new List<MyModel1>();                    
MyUserModel myUserModel =  new MyUserModel();                    
List<MyModel2> myModel2 = new List<MyModel1>();
myModel1 = m_Service1.GetMyModelFields();
myUserModel = m_Service2.GetMyUserDetails();
myModel2 = (from myModel1Field in myModel1                        
             select new MyModel2 { FieldCaption = myModel1Field.FieldAlias, 
             FieldValue = "" }).ToList<MyModel2>();

myModel1Field.FieldAlias text will be same as value of one of the Column attribute of one of the property in myUserModel. So I have to search for the column atribute(Name) in myUserModel and get the corresponding property values and assign it to 'FieldValue'. If I can't find the value in myUserModel I can set 'FieldValue' as "NA"

One way to get the column attribute(Name) value of for a property is as below when I know the Property name.

myUserModel.GetType().GetProperty("FirstName").GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute), false).Cast<System.Data.Linq.Mapping.ColumnAttribute>().Single().Name

But in my case Property name will not be known. I have to find the property based on the myModel1Field.FieldAlias value. How to go about this. Please suggest.

MyUserModel with one of it's properties

public class MyUserModel { 
[Column(Name = "first_name", DbType = "varchar")] 
public string FirstName { get; set; } 
}

Now if myModel1Field.FieldAlias is 'first_name' then I have to search in MyUserModel for a property with Column attribute(Name) as first_name. If it exists i have to set it's value to 'FieldValue'. Else set 'FieldValue' as "NA".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼角的笑意。 2025-01-04 20:12:22

如果您想获取某个属性的值,并且您只知道该属性的 ColumnAttribute 属性之一的 Name 属性,您可以执行以下操作:这

// Let's say you have the user model like so:
MyUserModel myUserModel = new MyUserModel { FirstName = "A", LastName = "B"};

// And then you want the value of the property that has the Column attribute Name "first_name"
string searchName = "first_name";    

// Using some lambda you can do this (I do not know how to do this in LINQ syntax, sorry)
object propertyValue = typeof (MyUserModel).GetProperties()
            .Where(p =>
                       {
                           var attrib = (ColumnAttribute)p
                               .GetCustomAttributes(typeof (ColumnAttribute), false)
                               .SingleOrDefault();
                           return (attrib != null && 
                                   attrib.Name.Equals(searchName));
                       })
            .Select(p => p.GetValue(myUserModel, null))
            .FirstOrDefault();

if(propertyValue != null)
{
    // Do whatever you want with the string "A" here - I suggest casting it to string! :-)
}

是您想要的吗?

If you want to get the value of a property and you only know the Name property of one of the ColumnAttribute attributes on it what you can do is this:

// Let's say you have the user model like so:
MyUserModel myUserModel = new MyUserModel { FirstName = "A", LastName = "B"};

// And then you want the value of the property that has the Column attribute Name "first_name"
string searchName = "first_name";    

// Using some lambda you can do this (I do not know how to do this in LINQ syntax, sorry)
object propertyValue = typeof (MyUserModel).GetProperties()
            .Where(p =>
                       {
                           var attrib = (ColumnAttribute)p
                               .GetCustomAttributes(typeof (ColumnAttribute), false)
                               .SingleOrDefault();
                           return (attrib != null && 
                                   attrib.Name.Equals(searchName));
                       })
            .Select(p => p.GetValue(myUserModel, null))
            .FirstOrDefault();

if(propertyValue != null)
{
    // Do whatever you want with the string "A" here - I suggest casting it to string! :-)
}

Is that what you want?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文