小编zra*_*zdn的帖子

从枚举中获取XmlEnumAttribute

我有枚举:

public enum Operation {
    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("01")]
    Item01,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("02")]
    Item02,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("03")]
    Item03,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("04")]
    Item04,
}
Run Code Online (Sandbox Code Playgroud)

我如何获得XmlEnumAttribute值?

我正在努力:

var res = Operation.Item1;
var result = (res.GetType().GetField("Item01").GetCustomAttributes(typeof(XmlEnumAttribute), true)[0] as XmlEnumAttribute).Name;
Run Code Online (Sandbox Code Playgroud)

可能存在更好的方法吗?

c# enums

15
推荐指数
3
解决办法
1万
查看次数

枚举到最佳实践

我不能在两种转换方法之间进行选择.从枚举转换为int的最佳实践是什么?

1:

public static int EnumToInt(Enum enumValue)
{
    return Convert.ToInt32(enumValue);
}
Run Code Online (Sandbox Code Playgroud)

2:

public static int EnumToInt(Enum enumValue)
{
    return (int)(ValueType)enumValue;
}
Run Code Online (Sandbox Code Playgroud)

c# enums

12
推荐指数
1
解决办法
1355
查看次数

LINQ to Entities不支持LINQ表达式节点类型"Lambda"

我正在尝试使用Expression并传递给查询.但我有错误 - LINQ to Entities不支持LINQ表达式节点类型'Lambda'.我也使用linqkit.dll和AsExpandable(),但是有相同的错误.

public List<Correct> GetCorrects(Expression<Func<Correct, bool?>> predicate)
{
    using (SystemsEntities context = new SystemsEntities())
    {
        var result = context.Corrects.Where(x => predicate == null);
        return result.ToList();
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到上述错误.什么失败了?

c# linq entity-framework

8
推荐指数
1
解决办法
2550
查看次数

实体框架DbContext更新值无需查询和外键

I have a method for updating some tables. For update I need get first of TestProcess, but I don't like that. How can I update TestProcess without select(firstOrDefault) operation, used only for the update operation?

Example of method:

public void UpdateTestProcess(int id, string updateID)
{
    using (TestEntities context = new TestEntities())
                {
                    TestProcess pr = context.TestProcess.FirstOrDefault(x => x.MyID == id);
                    pr.UpdateID = updateID;             

                    context.TestProcess.Attach(pr);
                    context.ObjectStateManager.ChangeObjectState(pr, EntityState.Modified);
                    context.SaveChanges();
               }
}
Run Code Online (Sandbox Code Playgroud)

c# updates dbcontext entity-framework-5

4
推荐指数
1
解决办法
7039
查看次数

List <T>的手写映射器

如何手写映射最好的方式List<T1>List<T2>

我的示例类:

public class T1
{
    public int ID {get; set}
    public string Name {get; set}
}

public class T2
{
    public int ID {get; set}
    public string Name {get; set}
}
Run Code Online (Sandbox Code Playgroud)

c# mapping

2
推荐指数
1
解决办法
576
查看次数

循环遍历没有反射的对象属性

我有MyModel类和MyModel的一些对象.

我需要没有反射的对象的for循环或foreach属性.如何实施?

类示例:

public class MyModel
{
    public string Level1_TypeName { get; set; }
    public string Level1_AttrType { get; set; }
    public string Level1_AttrValue { get; set; }
    public string Level2_TypeName { get; set; } 
    public string Level2_AttrType { get; set; }
    public string Level2_AttrValue { get; set; }
    public string Level3_TypeName { get; set; } 
    public string Level3_AttrType { get; set; }
    public string Level3_AttrValue { get; set; }
    public string Level4_TypeName { get; set; } 
    public string Level4_AttrType …
Run Code Online (Sandbox Code Playgroud)

c# foreach for-loop

-1
推荐指数
1
解决办法
1776
查看次数