在c#中,Foreach语句不包含GetEnumerator的公共定义
本文关键字:GetEnumerator 定义 包含 Foreach 语句 | 更新日期: 2023-09-27 17:54:35
我在使用Windows store 8.1应用程序时遇到了问题。错误提示"foreach语句不能操作类型为'WindowsStore.WCF '的变量"。passellerepleyofarrayofuserresponsedto ' because 'WindowsStore.WCF。passellerepleyofarrayofuserresponsedto '不包含'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);
}
但是我有一个异常:一个类型为'System '的异常。NullReferenceException'在WindowsStore.exe中发生,但未在用户代码中处理
try with
foreach(var Cand in Candidat.Data)
{
}