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


C# MappingSchema.GetMapValues方法代码示例

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


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

示例1: 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


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