猿问

无法在具有 CustomType 的查询中转换为 IConvertible

如果你想使用 LINQ:


var resultArray = a.Split(',').Concat(b.Split(',')).ToArray();

或者不使用 LINQ,您可以将连接作为字符串,然后使用以下 3 行之一进行分割:


var resultArray = $"{a},{b}".Split(','); //c#6+ String interpolation-formatted style

var resultArray = string.Format("{0},{1}", a, b).Split(','); //c# any version, formatted style

var resultArray = (a+","+b).Split(','); //concat style

或者您可以将它们加载到列表中,然后将其转换为数组:


var l = new List<string>(a.Split(','));

l.AddRange(b.Split(','));

var resultArray = l.ToArray();

这绝不是一个详尽的列表,但它详细介绍了使用 LINQ 执行此操作的最简单方法(如果您有多种不同类型的枚举,则很容易),而不使用 LINQ(如果它确实是一对短字符串,并且您想要一个易于阅读的片段),带有一个集合(如果您想传递它并从不同的地方填充它)


如果情况真的像您这里所看到的那样,有几个短字符串,我会使用字符串连接然后拆分。string 类对“将 3 个字符串连接在一起”操作进行了特定的优化,因此它应该具有合理的性能、代码简短且易于理解。如果您要执行数百万个这样的操作,那么拆分为两个数组可能会更快,创建第三个数组,其长度与 a 和 b 长度数组一样长,然后将 a 复制到起始位置,将 b 复制到偏移量 a 处。长度


猛跑小猪
浏览 79回答 1
1回答

慕妹3242003

解决方案是为不同的值对象创建我自己的自定义类型:public sealed class EmployeeNumberUserType : SingleValueObjectType<EmployeeNumber>{&nbsp; &nbsp; protected override NullableType PrimitiveType => NHibernateUtil.Int32;}public abstract class SingleValueObjectType<TValueObject> : IUserType where TValueObject : class{&nbsp; &nbsp; public SqlType[] SqlTypes => new[] { PrimitiveType.SqlType };&nbsp; &nbsp; public Type ReturnedType => typeof(TValueObject);&nbsp; &nbsp; public bool IsMutable => false;&nbsp; &nbsp; public object Assemble(object cached, object owner) => cached;&nbsp; &nbsp; public object DeepCopy(object value) => value;&nbsp; &nbsp; public object Disassemble(object value) => value;&nbsp; &nbsp; public new bool Equals(object x, object y) => x?.Equals(y) ?? y?.Equals(x) ?? true;&nbsp; &nbsp; public int GetHashCode(object x) => x?.GetHashCode() ?? 0;&nbsp; &nbsp; public object NullSafeGet(DbDataReader rs, string[] names, ISessionImplementor session, object owner)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var obj = PrimitiveType.NullSafeGet(rs, names[0], session, owner);&nbsp; &nbsp; &nbsp; &nbsp; if (obj == null) return null;&nbsp; &nbsp; &nbsp; &nbsp; else return Activator.CreateInstance(typeof(TValueObject), obj);&nbsp; &nbsp; }&nbsp; &nbsp; public void NullSafeSet(DbCommand cmd, object value, int index, ISessionImplementor session)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (value == null) cmd.Parameters[index].Value = DBNull.Value;&nbsp; &nbsp; &nbsp; &nbsp; else cmd.Parameters[index].Value = value.GetType()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GetProperty("Value", BindingFlags.Instance | BindingFlags.NonPublic)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GetValue(value);&nbsp; &nbsp; }&nbsp; &nbsp; public object Replace(object original, object target, object owner) => original;&nbsp; &nbsp; protected abstract NullableType PrimitiveType { get; }}并在映射中明确使用此自定义类型:public class OrderMap : ClassMap<Order>{&nbsp; &nbsp; public OrderMap()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Id(Entity.Expressions<Order>.Id);&nbsp; &nbsp; &nbsp; &nbsp; Map(x => x.EmployeeNumber)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Unique()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .CustomType<EmployeeNumberUserType>();&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答