代码之家  ›  专栏  ›  技术社区  ›  netmajor

在WCF中发送自己的数据收集

  •  0
  • netmajor  · 技术社区  · 14 年前

    我有自己的收集数据合同:

        [DataContract]
    public class DzieckoAndOpiekun
    {
        public DzieckoAndOpiekun() { }
        public DzieckoAndOpiekun(string dzImie, string dzNazwisko, string opImie, string opNazwisko)
        {
            this.DzieckoImie = dzImie;
            this.DzieckoImie = dzNazwisko;
            this.OpiekunImie = opImie;
            this.OpiekunNazwisko = opNazwisko;
        }
    
        /// <summary>
        /// Field for Dziecko's Surname
        /// </summary>
        private string dzieckoNazwisko;
    
        /// <summary>
        /// Field for Dziecko's Name
        /// </summary>
        private string dzieckoImie;
    
        /// <summary>
        /// Field for Opiekun's Surname
        /// </summary>
        private string opiekunNazwisko;
    
        /// <summary>
        /// Field for Opiekun's Name
        /// </summary>
        private string opiekunImie;
    
        /// <summary>
        /// Gets or sets the dziecko nazwisko.
        /// </summary>
        /// <value>The dziecko nazwisko.</value>
        [DataMember]
        public virtual string DzieckoNazwisko
        {
            get { return this.dzieckoNazwisko; }
            set { this.dzieckoNazwisko = value; }
        }
    
        /// <summary>
        /// Gets or sets the dziecko imie.
        /// </summary>
        /// <value>The dziecko imie.</value>
        [DataMember]
        public virtual string DzieckoImie
        {
            get { return this.dzieckoImie; }
            set { this.dzieckoImie = value; }
        }
    
        /// <summary>
        /// Gets or sets the opiekun nazwisko.
        /// </summary>
        /// <value>The opiekun nazwisko.</value>
        [DataMember]
        public virtual string OpiekunNazwisko
        {
            get { return this.opiekunNazwisko; }
            set { this.opiekunNazwisko = value; }
        }
    
        /// <summary>
        /// Gets or sets the opiekun imie.
        /// </summary>
        /// <value>The opiekun imie.</value>
        [DataMember]
        public virtual string OpiekunImie
        {
            get { return this.opiekunImie; }
            set { this.opiekunImie = value; }
        }
    }
    
    [DataContract]
    public class DzieckoAndOpiekunCollection : IEnumerable<DzieckoAndOpiekun>, IList<DzieckoAndOpiekun>
    {
        [DataMember]
        List<DzieckoAndOpiekun> collection = new List<DzieckoAndOpiekun>();
    
        [DataMember]
        public List<DzieckoAndOpiekun> Collection
        {
            get { return collection; }
            set { collection = value; }
        }
    
        public IEnumerator<DzieckoAndOpiekun> GetEnumerator()
        {
            return collection.GetEnumerator();
        }
    
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new System.NotImplementedException();
        }
    
        public int IndexOf(DzieckoAndOpiekun item)
        {
            return collection.IndexOf(item);
        }
    
        public void Insert(int index, DzieckoAndOpiekun item)
        {
            collection.Insert(index, item);
        }
    
        public void RemoveAt(int index)
        {
            collection.RemoveAt(index);
        }
    
        public DzieckoAndOpiekun this[int index]
        {
            get
            {
                return collection[index];
            }
            set
            {
                collection[index] = value;
            }
        }
    
        public void Add(DzieckoAndOpiekun item)
        {
            this.collection.Add(item);
        }
    
        public void AddRange(IList<DzieckoAndOpiekun> collection)
        {
            this.collection.AddRange(collection);
        }
    
        public void Clear()
        {
            collection.Clear();
        }
    
        public bool Contains(DzieckoAndOpiekun item)
        {
            return collection.Contains(item);
        }
    
        public void CopyTo(DzieckoAndOpiekun[] array, int arrayIndex)
        {
            this.collection.CopyTo(array, arrayIndex);
        }
    
        public int Count
        {
            get { return this.collection.Count; }
        }
    
        public bool IsReadOnly
        {
            get { return false; }
        }
    
        public bool Remove(DzieckoAndOpiekun item)
        {
            return this.collection.Remove(item);
        }
    }
    

     public DzieckoAndOpiekunCollection GetChildAndOpiekunByFirstnameLastname(string firstname, string lastname)
        {
            DataTransfer.ChargeInSchoolEntities db = new DataTransfer.ChargeInSchoolEntities();
            DzieckoAndOpiekunCollection result = new DzieckoAndOpiekunCollection();
            if (firstname == null && lastname != null)
            {
                IQueryable<DzieckoAndOpiekun> resultV = from p in db.Dziecko
                                                        where lastname == p.Nazwisko
                                                        select new DzieckoAndOpiekun(
                                                       p.Imie,
                                                       p.Nazwisko,
                                                       p.Opiekun.Imie,
                                                       p.Opiekun.Nazwisko);
                List<DzieckoAndOpiekun> l = resultV.ToList();
                result.AddRange(l);
            }
            if (firstname != null && lastname != null)
            {
                IQueryable<DzieckoAndOpiekun> resultV = from p in db.Dziecko
                                                        where firstname == p.Imie && lastname == p.Nazwisko
                                                        select new DzieckoAndOpiekun(
                                                       p.Imie,
                                                       p.Nazwisko,
                                                       p.Opiekun.Imie,
                                                       p.Opiekun.Nazwisko);
                List<DzieckoAndOpiekun> l = resultV.ToList();
                result.AddRange(l);
            }
            if (firstname != null && lastname == null)
            {
                IQueryable<DzieckoAndOpiekun> resultV = from p in db.Dziecko
                                                        where firstname == p.Imie
                                                        select new DzieckoAndOpiekun(
                                                       p.Imie,
                                                       p.Nazwisko,
                                                       p.Opiekun.Imie,
                                                       p.Opiekun.Nazwisko);
                List<DzieckoAndOpiekun> l = resultV.ToList();
                result.AddRange(l);
            }
            return result;
        }
    

    我是否在创建自己的数据契约并发送它时出错了?我问,因为我在使用方法时出错:/:

    操作期间发生异常,导致结果无效。 细节。 在 System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() 在 SecretaryAppNav.ClientService.GetChildAndOpiekunByFirstnameLastnameCompletedEventArgs.get\u Result()位于 SecretaryAppNav.Views.FindChild.Client\u GetChildAndOpiekunByFirstnameLastnameCompleted(对象 GetChildAndOpiekunByFirstnameLastnameCompletedEventArgs 电子)
    (州)

    2 回复  |  直到 14 年前
        1
  •  2
  •   casperOne    14 年前

    我不能保证这会起作用,但你要发送两次收集:

    [DataContract]
    public class DzieckoAndOpiekunCollection : 
        IEnumerable<DzieckoAndOpiekun>, IList<DzieckoAndOpiekun>
    {
        [DataMember]
        List<DzieckoAndOpiekun> collection = new List<DzieckoAndOpiekun>();
    
        [DataMember]
        public List<DzieckoAndOpiekun> Collection
        {
            get { return collection; }
            set { collection = value; }
        }
    ...
    

    您可能只应该将DataMember属性放在字段或属性上,而不能同时放在两者上,因为它们公开了相同的值。

        2
  •  0
  •   netmajor    14 年前

    我解决了CollectionDataContract属性的问题;)