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


C# Dictionary.Single方法代码示例

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


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

示例1: rpt_OnItemCommand

 //生成订单并跳转
 protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
 {
     switch (e.CommandName)
     {
         case "order":
         {
             var rpt = e.Item.FindControl("rptProduct") as Repeater;
             var shopId = e.CommandArgument.ToString();
             var cookieName = "shop_" + shopId;
             var lblList = new Dictionary<string,string>();//产品Id和数量键值对
             rpt.Controls.OfType<RepeaterItem>().ToList().ForEach(rItem=>
             {
                 if (rItem.ItemType==ListItemType.Item || rItem.ItemType == ListItemType.AlternatingItem)
                 {
                     lblList.Add((rItem.FindControl("lblAmount") as Label).Attributes["data-pid"],(rItem.FindControl("hfAmount") as HiddenField).Value);
                 }
             });
             var length = Request.Cookies[cookieName].Values.Keys.Count;
             int p_num = 0;
             for (int i = 0; i < length; i++)
             {
                 var httpCookie = Request.Cookies[cookieName];
                 if (httpCookie != null)
                 {
                     var key = Request.Cookies[cookieName].Values.Keys[i];
                     string tmpStr=Request.Cookies[cookieName].Values[key];
                     int tmpInt = Convert.ToInt32(tmpStr);
                     p_num += tmpInt;
                     httpCookie.Values[key] = lblList.Single(p => p.Key == key).Value;
                 }
             }
             if (p_num <= 0)
             {
                 WebUtil.Alert("数量必须大于0");
                 return;
             }
             Response.Redirect("UserSubmitOrder.aspx?shopid="+shopId);
             break;
         }
     }
 }
开发者ID:836146482,项目名称:ABC,代码行数:42,代码来源:UserShopCart.aspx.cs

示例2: Serialize

        /// <summary>
        /// Serializes the given graph to the given filepath using Xml data format. 
        /// </summary>
        internal static void Serialize(RDFGraph graph, String filepath) {
            try {

                #region serialize
                using (XmlTextWriter rdfxmlWriter = new XmlTextWriter(filepath, Encoding.UTF8))  {
                    XmlDocument rdfDoc            = new XmlDocument();
                    rdfxmlWriter.Formatting       = Formatting.Indented;

                    #region xmlDecl
                    XmlDeclaration xmlDecl        = rdfDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                    rdfDoc.AppendChild(xmlDecl);
                    #endregion

                    #region rdfRoot
                    XmlNode rdfRoot               = rdfDoc.CreateNode(XmlNodeType.Element, RDFVocabulary.RDF.PREFIX + ":RDF", RDFVocabulary.RDF.BASE_URI);
                    XmlAttribute rdfRootNS        = rdfDoc.CreateAttribute("xmlns:" + RDFVocabulary.RDF.PREFIX);
                    XmlText rdfRootNSText         = rdfDoc.CreateTextNode(RDFVocabulary.RDF.BASE_URI);
                    rdfRootNS.AppendChild(rdfRootNSText);
                    rdfRoot.Attributes.Append(rdfRootNS);

                    #region prefixes
                    //Write the graph's prefixes (except for "rdf", which has already been written)
                    graph.GraphMetadata.Namespaces.ForEach(p => {
                        if (!p.Prefix.Equals(RDFVocabulary.RDF.PREFIX, StringComparison.Ordinal) && !p.Prefix.Equals("base", StringComparison.Ordinal)) {
                            XmlAttribute pfRootNS     = rdfDoc.CreateAttribute("xmlns:" + p.Prefix);
                            XmlText pfRootNSText      = rdfDoc.CreateTextNode(p.ToString());
                            pfRootNS.AppendChild(pfRootNSText);
                            rdfRoot.Attributes.Append(pfRootNS);
                        }
                    });
                    //Write the graph's base uri to resolve eventual relative #IDs
                    XmlAttribute pfBaseNS             = rdfDoc.CreateAttribute(RDFVocabulary.XML.PREFIX + ":base");
                    XmlText pfBaseNSText              = rdfDoc.CreateTextNode(graph.Context.ToString());
                    pfBaseNS.AppendChild(pfBaseNSText);
                    rdfRoot.Attributes.Append(pfBaseNS);
                    #endregion

                    #region linq
                    //Group the graph's triples by subj
                    var groupedList =  (from    triple in graph
                                        orderby triple.Subject.ToString()
                                        group   triple by new {
                                            subj = triple.Subject.ToString()
                                        });
                    #endregion

                    #region graph
                    //Iterate over the calculated groups
                    Dictionary<RDFResource, XmlNode> containers = new Dictionary<RDFResource, XmlNode>();
                    
                    //Floating containers have reification subject which is never object of any graph's triple
                    Boolean floatingContainers                  = graph.GraphMetadata.Containers.Keys.Any(k =>
                                                                        graph.Triples.Values.Count(v => v.Object.Equals(k)) == 0);
                    //Floating collections have reification subject which is never object of any graph's triple
                    Boolean floatingCollections                 = graph.GraphMetadata.Collections.Keys.Any(k => 
                                                                        graph.Triples.Values.Count(v => v.Object.Equals(k)) == 0);

                    foreach (var group in groupedList) {

                        #region subj
                        //Check if the current subj is a container or a collection subj: if so it must be
                        //serialized in the canonical RDF/XML way instead of the "rdf:Description" way
                        XmlNode subjNode              = null;
                        String subj                   = group.Key.subj;

                        //It is a container subj, so add it to the containers pool
                        if (graph.GraphMetadata.Containers.Keys.Any(k => k.ToString().Equals(subj, StringComparison.Ordinal)) && !floatingContainers) {
                            switch (graph.GraphMetadata.Containers.Single(c => c.Key.ToString().Equals(subj, StringComparison.Ordinal)).Value) {
                                case RDFModelEnums.RDFContainerTypes.Bag:
                                    subjNode  = rdfDoc.CreateNode(XmlNodeType.Element, RDFVocabulary.RDF.PREFIX + ":Bag", RDFVocabulary.RDF.BASE_URI);
                                    containers.Add(new RDFResource(subj), subjNode);
                                    break;
                                case RDFModelEnums.RDFContainerTypes.Seq:
                                    subjNode  = rdfDoc.CreateNode(XmlNodeType.Element, RDFVocabulary.RDF.PREFIX + ":Seq", RDFVocabulary.RDF.BASE_URI);
                                    containers.Add(new RDFResource(subj), subjNode);
                                    break;
                                case RDFModelEnums.RDFContainerTypes.Alt:
                                    subjNode  = rdfDoc.CreateNode(XmlNodeType.Element, RDFVocabulary.RDF.PREFIX + ":Alt", RDFVocabulary.RDF.BASE_URI);
                                    containers.Add(new RDFResource(subj), subjNode);
                                    break;
                            }
                        }

                        //It is a subj of a collection of resources, so do not append triples having it as a subject
                        //because we will reconstruct the collection and append it as a whole
                        else if (graph.GraphMetadata.Collections.Keys.Any(k => k.ToString().Equals(subj, StringComparison.Ordinal))                                                         &&
                                 graph.GraphMetadata.Collections.Single(c => c.Key.ToString().Equals(subj, StringComparison.Ordinal)).Value.ItemType == RDFModelEnums.RDFItemTypes.Resource &&
                                 !floatingCollections) {
                            continue;
                        }

                        //It is neither a container or a collection subj
                        else {
                            subjNode                       = rdfDoc.CreateNode(XmlNodeType.Element, RDFVocabulary.RDF.PREFIX + ":Description", RDFVocabulary.RDF.BASE_URI);
                            //<rdf:Description rdf:nodeID="blankID">
                            XmlAttribute subjNodeDesc      = null;
                            XmlText subjNodeDescText       = rdfDoc.CreateTextNode(group.Key.subj);
//.........这里部分代码省略.........
开发者ID:Acidburn0zzz,项目名称:RDFSharp,代码行数:101,代码来源:RDFXml.cs


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