当前位置: 首页>>代码示例>>C#>>正文


C# MappingSchema.GetDefaultValue方法代码示例

本文整理汇总了C#中MappingSchema.GetDefaultValue方法的典型用法代码示例。如果您正苦于以下问题:C# MappingSchema.GetDefaultValue方法的具体用法?C# MappingSchema.GetDefaultValue怎么用?C# MappingSchema.GetDefaultValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MappingSchema的用法示例。


在下文中一共展示了MappingSchema.GetDefaultValue方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetColumnReader

		static Expression GetColumnReader(
			IDataContext dataContext, MappingSchema mappingSchema, IDataReader dataReader, Type type, int idx, Expression dataReaderExpr)
		{
			var ex = dataContext.GetReaderExpression(mappingSchema, dataReader, idx, dataReaderExpr, type.ToNullableUnderlying());

			if (ex.NodeType == ExpressionType.Lambda)
			{
				var l = (LambdaExpression)ex;

				switch (l.Parameters.Count)
				{
					case 1 : ex = l.GetBody(dataReaderExpr);                break;
					case 2 : ex = l.GetBody(dataReaderExpr, Constant(idx)); break;
				}
			}

			var conv = mappingSchema.GetConvertExpression(ex.Type, type, false);

			// Replace multiple parameters with single variable or single parameter with the reader expression.
			//
			if (conv.Body.GetCount(e => e == conv.Parameters[0]) > 1)
			{
				var variable = Variable(ex.Type);
				var assign   = Assign(variable, ex);

				ex = Block(new[] { variable }, new[] { assign, conv.GetBody(variable) });
			}
			else
			{
				ex = conv.GetBody(ex);
			}

			// Add check null expression.
			//
			if (dataContext.IsDBNullAllowed(dataReader, idx) ?? true)
			{
				ex = Condition(
					Call(dataReaderExpr, _isDBNullInfo, Constant(idx)),
					Constant(mappingSchema.GetDefaultValue(type), type),
					ex);
			}

			return ex;
		}
开发者ID:donners77,项目名称:linq2db,代码行数:44,代码来源:ConvertFromDataReaderExpression.cs

示例2: GetToEnum

        static Expression GetToEnum(Type @from, Type to, Expression expression, MappingSchema mappingSchema)
        {
            if (to.IsEnumEx())
            {
                var toFields = mappingSchema.GetMapValues(to);

                if (toFields == null)
                    return null;

                var fromTypeFields = toFields
                    .Select(f => new { f.OrigValue, attrs = f.MapValues.Where(a => a.Value == null || a.Value.GetType() == @from).ToList() })
                    .ToList();

                if (fromTypeFields.All(f => f.attrs.Count != 0))
                {
                    var cases = fromTypeFields
                        .Select(f => new
                            {
                                value = f.OrigValue,
                                attrs = f.attrs
                                    .Where (a => a.Configuration == f.attrs[0].Configuration)
                                    .Select(a => a.Value ?? mappingSchema.GetDefaultValue(@from))
                                    .ToList()
                            })
                        .ToList();

                    var ambiguityMappings =
                        from c in cases
                        from a in c.attrs
                        group c by a into g
                        where g.Count() > 1
                        select g;

                    var ambiguityMapping = ambiguityMappings.FirstOrDefault();

                    if (ambiguityMapping != null)
                    {
                        var enums = ambiguityMapping.ToArray();

                        return Expression.Convert(
                            Expression.Call(
                                _throwLinqToDBConvertException,
                                Expression.Constant(
                                    "Mapping ambiguity. MapValue({0}) attribute is defined for both '{1}.{2}' and '{1}.{3}'."
                                        .Args(ambiguityMapping.Key, to.FullName, enums[0].value, enums[1].value))),
                                to);
                    }

                    var expr = Expression.Switch(
                        expression,
                        Expression.Convert(
                            Expression.Call(_defaultConverter,
                                Expression.Convert(expression, typeof(object)),
                                Expression.Constant(to)),
                            to),
                        cases
                            .Select(f =>
                                Expression.SwitchCase(
                                    Expression.Constant(f.value),
                                    (IEnumerable<Expression>)f.attrs.Select(a => Expression.Constant(a, @from))))
                            .ToArray());

                    return expr;
                }

                if (fromTypeFields.Any(f => f.attrs.Count(a => a.Value != null) != 0))
                {
                    var field = fromTypeFields.First(f => f.attrs.Count == 0);

                    return Expression.Convert(
                        Expression.Call(
                            _throwLinqToDBConvertException,
                            Expression.Constant(
                                "Inconsistent mapping. '{0}.{1}' does not have MapValue(<{2}>) attribute."
                                    .Args(to.FullName, field.OrigValue, from.FullName))),
                            to);
                }
            }

            return null;
        }
开发者ID:Convey-Compliance,项目名称:linq2db,代码行数:81,代码来源:ConvertBuilder.cs

示例3: GetFromEnum

        static Expression GetFromEnum(Type @from, Type to, Expression expression, MappingSchema mappingSchema)
        {
            if (from.IsEnumEx())
            {
                var fromFields = @from.GetFieldsEx()
                    .Where (f => (f.Attributes & EnumField) == EnumField)
                    .Select(f => new EnumValues { Field = f, Attrs = mappingSchema.GetAttributes<MapValueAttribute>(f, a => a.Configuration) })
                    .ToList();

                {
                    var toTypeFields = fromFields
                        .Select(f => new { f.Field, Attrs = f.Attrs
                            .OrderBy(a =>
                            {
                                var idx = a.Configuration == null ?
                                    int.MaxValue :
                                    Array.IndexOf(mappingSchema.ConfigurationList, a.Configuration);
                                return idx < 0 ? int.MaxValue : idx;
                            })
                            .ThenBy(a => !a.IsDefault)
                            .ThenBy(a => a.Value == null)
                            .FirstOrDefault(a => a.Value == null || a.Value.GetType() == to) })
                        .ToList();

                    if (toTypeFields.All(f => f.Attrs != null))
                    {
                        var cases = toTypeFields.Select(f => Expression.SwitchCase(
                            Expression.Constant(f.Attrs.Value ?? mappingSchema.GetDefaultValue(to), to),
                            Expression.Constant(Enum.Parse(@from, f.Field.Name, false))));

                        var expr = Expression.Switch(
                            expression,
                            Expression.Convert(
                                Expression.Call(_defaultConverter,
                                    Expression.Convert(expression, typeof(object)),
                                    Expression.Constant(to)),
                                to),
                            cases.ToArray());

                        return expr;
                    }

                    if (toTypeFields.Any(f => f.Attrs != null))
                    {
                        var field = toTypeFields.First(f => f.Attrs == null);

                        return Expression.Convert(
                            Expression.Call(
                                _throwLinqToDBConvertException,
                                Expression.Constant(
                                    "Inconsistent mapping. '{0}.{1}' does not have MapValue(<{2}>) attribute."
                                        .Args(from.FullName, field.Field.Name, to.FullName))),
                                to);
                    }
                }

                if (to.IsEnumEx())
                {
                    var toFields = to.GetFieldsEx()
                        .Where (f => (f.Attributes & EnumField) == EnumField)
                        .Select(f => new EnumValues { Field = f, Attrs = mappingSchema.GetAttributes<MapValueAttribute>(f, a => a.Configuration) })
                        .ToList();

                    var dic = new Dictionary<EnumValues,EnumValues>();
                    var cl  = mappingSchema.ConfigurationList.Concat(new[] { "", null }).Select((c,i) => new { c, i }).ToArray();

                    foreach (var toField in toFields)
                    {
                        if (toField.Attrs == null || toField.Attrs.Length == 0)
                            return null;

                        var toAttr = toField.Attrs.First();

                        toAttr = toField.Attrs.FirstOrDefault(a => a.Configuration == toAttr.Configuration && a.IsDefault) ?? toAttr;

                        var fromAttrs = fromFields.Where(f => f.Attrs.Any(a =>
                            a.Value == null ? toAttr.Value == null : a.Value.Equals(toAttr.Value))).ToList();

                        if (fromAttrs.Count == 0)
                            return null;

                        if (fromAttrs.Count > 1)
                        {
                            var fattrs =
                                from f in fromAttrs
                                select new {
                                    f,
                                    a = f.Attrs.First(a => a.Value == null ? toAttr.Value == null : a.Value.Equals(toAttr.Value))
                                } into fa
                                from c in cl
                                where fa.a.Configuration == c.c
                                orderby c.i
                                select fa.f;

                            fromAttrs = fattrs.Take(1).ToList();
                        }

                        var prev = dic
                            .Where (a => a.Value.Field == fromAttrs[0].Field)
                            .Select(pair => new { To = pair.Key, From = pair.Value })
//.........这里部分代码省略.........
开发者ID:Convey-Compliance,项目名称:linq2db,代码行数:101,代码来源:ConvertBuilder.cs

示例4: ColumnReader

			public ColumnReader(IDataContext dataContext, MappingSchema mappingSchema, Type columnType, int columnIndex)
			{
				_dataContext  = dataContext;
				_mappingSchema = mappingSchema;
				_columnType    = columnType;
				_columnIndex   = columnIndex;
				_defaultValue  = mappingSchema.GetDefaultValue(columnType);
			}
开发者ID:donners77,项目名称:linq2db,代码行数:8,代码来源:ConvertFromDataReaderExpression.cs

示例5: ConvertNullableEnum

		public void ConvertNullableEnum()
		{
			var schema = new MappingSchema("2");
			Assert.That(schema.GetDefaultValue(typeof(Enum1?)), Is.Null);
			var mapType = ConvertBuilder.GetDefaultMappingFromEnumType(schema, typeof(Enum1?));
			Assert.That(mapType, Is.EqualTo(typeof(int?)));
			var convertedValue = Converter.ChangeType(null, mapType, schema);
			Assert.IsNull(convertedValue);
		}
开发者ID:jkshan,项目名称:linq2db,代码行数:9,代码来源:MappingSchemaTest.cs


注:本文中的MappingSchema.GetDefaultValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。