2

我需要在 C# 中列出生产实体中的成员。

下面是代码,但我不知道如何从集合中构建成员属性列表,例如名称。代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// additional references... 
using HelloMDS.MDService; /* for the created service reference */
using System.Collections.ObjectModel; /* supports collection objects used in the proxy */

namespace HelloMDSG_Members
{
    class Program
    {
        private static ServiceClient mdsProxy; /* service proxy object */

        static void Main(string[] args)
        {
            // Create the service proxy 
            Console.WriteLine("Connecting...");
            try
            {
                mdsProxy = CreateMdsProxy("http://localhost/MDS/service/Service.svc");
                Console.WriteLine("Connected.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error connecting: " + ex.Message);
            }

            Console.WriteLine("Call GetMembers");
            GetMembers();

            Console.WriteLine("Finished");
            Console.ReadKey();

        }

        public static void GetMembers()
        {


            //declare a new MDS ServiceClient
            ServiceClient client = new ServiceClient();
            //EntityMembersGetRequest request = new EntityMembersGetRequest();

            //Build a request for Entity Members of the Product Entity 
            //in the Product Model, Version 4 from the MDS Sample package on the MS Connect site
            EntityMembersGetRequest request = new EntityMembersGetRequest();
            request.MembersGetCriteria = new EntityMembersGetCriteria();
            request.MembersGetCriteria.ModelId = new Identifier() { Name = "Model 1" };
            request.MembersGetCriteria.VersionId = new Identifier() { Name = "VERSION_1" };
            request.MembersGetCriteria.EntityId = new Identifier() { Name = "Product" };

            request.MembersGetCriteria.MemberReturnOption = MemberReturnOption.DataAndCounts; //without this the request doesn't return memebers !

            Console.WriteLine("Start getting data");

            //submit the request to the MDS Web service  Client
            EntityMembersGetResponse response = client.EntityMembersGet(request);

            Console.WriteLine("Count:{0}", response.EntityMembersInformation.MemberCount);

            //confirm that members were returned
            if (response.EntityMembersInformation.MemberCount > 0)
            {
                System.Collections.ObjectModel.Collection<Member> members = response.EntityMembers.Members;

                // HELP HERE PLEASE
                // write members list with the name attribute
                Console.WriteLine() member  - name attribute );               

            }

            HandleErrors(response.OperationResult);
        }

        // creates the service client proxy 
        private static ServiceClient CreateMdsProxy(string mdsURL)
        {
            // create an endpoint address using the URL 
            System.ServiceModel.EndpointAddress endptAddress = new System.ServiceModel.EndpointAddress(mdsURL);

            // create and configure the WS Http binding 
            System.ServiceModel.WSHttpBinding wsBinding = new System.ServiceModel.WSHttpBinding();

            // create and return the client proxy 
            return new ServiceClient(wsBinding, endptAddress);
        }

        // Handles the operations results 
        private static void HandleErrors(OperationResult result)
        {
            string errorMessage = string.Empty;
            if (result.Errors.Count() != 0)
            {
                for (int i = 0; i <= result.Errors.Count() - 1; i++)
                {
                    errorMessage += " OperationResult:Error: " + result.Errors[i].Code + ":"
                         + result.Errors[i].Description + ":" + result.Errors[i].Context.Type.ToString();
                }
                Console.WriteLine("Error: " + errorMessage);
            }
        }
    }
}
4

1 回答 1

0

您是否在寻找:

foreach(var member in members)
{
    Console.WriteLine(member.MemberId.Id);
    foreach(var attribute in member.Attributes)
    {
        Console.WriteLine("Attribute: {0},\tValue: {1}", attribute.Identifier.Name, attribute.Value);
    }
}
于 2013-03-19T20:10:03.943 回答