Foreach语句不包含C#上GetEnumerator的公共定义

时间:2014-03-29 01:27:10

标签: c# wcf windows-store-apps

我遇到了Windows商店8.1应用程序的问题。该错误声明“foreach语句无法对'WindowsStore.WCF.PasserelleRepleyOFArrayOfUserResponseDTO'类型的变量进行操作,因为'WindowsStore.WCF.PasserelleRepleyOFArrayOfUserResponseDTO'不包含'GetEnumerator'的公共定义”。

这是抛出错误的代码:

 public sealed partial class MainPage : Page
{
    WCFService.WCFServiceClient MyService;
    public MainPage()
    {
        this.InitializeComponent();
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        MyService = new WCFService.WCFServiceClient();
    }
    private async void SearchBoxEventsQuerySubmitted(Windows.UI.Xaml.Controls.SearchBox sender,
SearchBoxQuerySubmittedEventArgs args)
    {
       var Candidat = await MyService.SearchCandidatesAsync(search.QueryText);
        UserResponseDTO user = new UserResponseDTO();
       foreach(Cand in Candidat)
       {

       }

    }

这是我的函数WebService:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WCFService : IWCFService
{
      public PasserelleReply<List<UserResponseDTO>> SearchCandidates(string CandidateCriterion)
    {
        Console.WriteLine("/SearchCandidates");
        DBEntities db = new DBEntities();
        var r = db.Person.Where(
            x => x.Lastname.Contains(CandidateCriterion) ||
            x.Firstname.Contains(CandidateCriterion) ||
            x.Login.Contains( CandidateCriterion )).ToList();
        if (r.Count() != 0)
        {
            return new PasserelleReply<List<UserResponseDTO>>(
                r.Select(y => Converter.PersonToUserResponseDTO(y)).ToList());
            //db.Dispose();
        }
        else return new PasserelleReply<List<UserResponseDTO>>(ReponseErreurService.NoUsers, "Aucun utilisateur n'est trouvé.");
    }
}

这是我的代码PasserelleReply:

[DataContract]
public class PasserelleReply<T> where T : class
{
    public PasserelleReply(ReponseErreurService codeErreur, string messageErreur)
    {
        this.CodeErreur = (int)codeErreur;
        this.MessageErreur = messageErreur;
    }

    public PasserelleReply(T data)
    {
        this.Data = data;
    }

    /// <summary>
    /// Temps d'exécution du service
    /// </summary>
    [DataMember]
    public double? ExecutionTime { get; set; }

    /// <summary>
    /// Chaque exception a son propre code d'erreur. <br/>  Une valeur nulle signifie pas d'exception.
    /// </summary>
    [DataMember]
    public int? CodeErreur { get; private set; }

    /// <summary>
    /// Réponse de passerelle. <br/> Valeur nulle en cas d'exception.
    /// </summary>
    [DataMember]
    public T Data { get; private set; }

    /// <summary>
    /// Identification du client.
    /// </summary>
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public string IdentifiantClient { get; set; }

    /// <summary>
    /// Les détails de l'exception seront disponibles ici.
    /// </summary>
    [DataMember]
    public string MessageErreur { get; private set; }

    /// <summary>
    /// Système client.
    /// </summary>
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public string Plateforme { get; set; }


    /// <summary>
    /// Method to call if we need to override the default error codes
    /// </summary>
    /// <param name="codeErreur"></param>
    /// <param name="messageErreur"></param>
    /// <returns></returns>
    public PasserelleReply<T> ResetError(ReponseErreurService codeErreur, string messageErreur)
    {
        this.CodeErreur = (int)codeErreur;
        this.MessageErreur = messageErreur;
        return this;
    }
}

这是我的代码User ResponseDTO:

[DataContract]
    public class UserResponseDTO:IEnumerable
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string Login { get; set; }

        [DataMember]
        public string Password { get; set; }

        [DataMember]
        public string Firsname { get; set; }

        [DataMember]
        public string Lastname { get; set; }

        [DataMember]
        public string Email { get; set; }

        [DataMember]
        public string Phone { get; set; }

        [DataMember]
        public List<RoleDTO> Roles { get; set; }

        [DataMember]
        public string OrigineName { get; set; }

        [DataMember]
        public string OrigineCode { get; set; }

    }

在我的应用程序中,我想搜索一个人,所以我想在gridview中显示我的候选人,但我有这个问题所以我不能使用游泳池

我这样做:

  private async void SearchBoxEventsQuerySubmitted(Windows.UI.Xaml.Controls.SearchBox sender,
SearchBoxQuerySubmittedEventArgs args)
    {
       var Candidat = await MyService.SearchCandidatesAsync(search.QueryText);

       foreach(var Cand in Candidat.Data)
       {
           GridViewItem RegView = new GridViewItem();
           StackPanel Spanel = new StackPanel();
           Spanel.Children.Add(new TextBlock() { Text = Cand.Firsname });
           Spanel.Children.Add(new TextBox() { Text = Cand.Email });

           RegView.Content = Spanel;
           candGridview.Items.Add(RegView);
       }

但我有一个例外:WindowsStore.exe中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理

1 个答案:

答案 0 :(得分:2)

尝试

foreach(var Cand in Candidat.Data)
{


}