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


C# ItemCollection.OfType方法代码示例

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


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

示例1: Generate

        /// <summary>
        /// Generates metadata for given item collection.
        /// Fetches CLR models from object item collection.
        /// </summary>
        /// <param name="metadataWorkspace">The metadata workspace.</param>
        /// <param name="itemCollection">The item collection.</param>
        /// <param name="objectItemCollection">The object item collection.</param>
        /// <param name="assembly">The assembly.</param>
        /// <param name="connectionString">The connection string.</param>
        /// <returns></returns>
        /// <exception cref="BeetleException">Cannot load mapping information.</exception>
        public static Metadata Generate(MetadataWorkspace metadataWorkspace, ItemCollection itemCollection,
                                        ObjectItemCollection objectItemCollection, Assembly assembly, string connectionString) {
            XDocument mappingXml = null;
            XNamespace mappingNs = null;
            try {
                var csResourceMatch = Regex.Match(connectionString, @"res:*/(.*?\.msl)");
                if (csResourceMatch.Success) {
                    var csResourceIndex = csResourceMatch.Value.LastIndexOf('/');
                    if (csResourceIndex >= 0) {
                        var csResource = csResourceMatch.Value.Substring(csResourceIndex + 1);
                        if (!string.IsNullOrEmpty(csResource)) {
                            using (var stream = assembly.GetManifestResourceStream(csResource)) {
                                if (stream != null) {
                                    using (var reader = new StreamReader(stream)) {
                                        mappingXml = XDocument.Load(reader);
                                        // ReSharper disable once PossibleNullReferenceException
                                        mappingNs = mappingXml.Root.GetDefaultNamespace();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch {
                mappingXml = null;
            }

            var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();
            // collect necessary entity information in one collection.
            var entityResources = new List<EntityResource>();
            foreach (var entityType in itemCollection.OfType<EntityType>().OrderBy(et => et.Name)) {
                var et = entityType;
                var entitySet = GetEntitySet(container, et);

                IEnumerable<KeyValuePair<string, string>> propertyMappings = null;
                XElement entityMap = null;
                if (mappingXml != null) {
                    entityMap = mappingXml
                        .Descendants(mappingNs.GetName("EntitySetMapping"))
                        .First(esm => esm.Attribute("Name").Value == entitySet.Name)
                        .Descendants(mappingNs.GetName("EntityTypeMapping"))
                        .First(mf => {
                            var typeName = mf.Attribute("TypeName").Value;
                            return typeName == et.FullName || typeName == string.Format("IsTypeOf({0})", et.FullName);
                        })
                        .Descendants(mappingNs.GetName("MappingFragment"))
                        .Single();
                    propertyMappings = entityMap.Descendants(mappingNs.GetName("ScalarProperty"))
                        .Select(sp => new KeyValuePair<string, string>(sp.Attribute("Name").Value, sp.Attribute("ColumnName").Value));
                }

                var complexProperties = et.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == et);
                Dictionary<EdmProperty, IEnumerable<ColumnMapping>> complexMappings = null;
                if (entityMap != null) {
                    complexMappings = new Dictionary<EdmProperty, IEnumerable<ColumnMapping>>();
                    foreach (var complexProperty in complexProperties) {
                        var complexMapping = entityMap
                            .Descendants(mappingNs.GetName("ComplexProperty"))
                            .First(cp => cp.Attribute("Name").Value == complexProperty.Name)
                            .Descendants(mappingNs.GetName("ScalarProperty"))
                            .Select(p => new ColumnMapping(p.Attribute("Name").Value, p.Attribute("ColumnName").Value));
                        complexMappings.Add(complexProperty, complexMapping);
                    }
                }

                var entityResource = new EntityResource {
                    Entity = et,
                    Type = et,
                    Name = et.Name,
                    EntitySet = entitySet,
                    SimpleProperties = et.Properties
                        .Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == et)
                        .ToDictionary(p => propertyMappings == null ? p.Name : propertyMappings.First(pm => pm.Key == p.Name).Value, p => p),
                    NavigationProperties = et.NavigationProperties.Where(np => np.DeclaringType == et),
                    ComplexProperties = complexMappings,
                    ClrType = objectItemCollection.GetClrType(objectItemCollection.OfType<EntityType>().First(x => x.Name == et.Name)),
                    TableName = entityMap == null ? et.Name : entityMap.Attribute("StoreEntitySet").Value
                };

                entityResources.Add(entityResource);
            }

            foreach (var complexType in itemCollection.OfType<ComplexType>().OrderBy(i => i.Name)) {
                var ct = complexType;
                var simpleProperties = ct.Properties
                    .Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == ct)
                    .ToDictionary(p => p.Name, p => p);
                var entityResource = new EntityResource {
//.........这里部分代码省略.........
开发者ID:amartelr,项目名称:Beetle.js,代码行数:101,代码来源:MetadataGenerator.cs


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