C#访问IEnumerable集合

时间:2013-01-04 18:57:30

标签: c# ienumerable

我对收藏品很新,所以请耐心等待我,我的行话甚至可能不准确。

我让PetaPoco将查询结果作为IEnumerable返回,每个结果都有一个集合。我想评估集合以从每个集合中的特定字段获取特定字符串。到目前为止,我能够迭代Enumerable,并且看起来能够根据我的代码片段访问一个对象,但是当我在调试中查看c.Language时,它只是字符串的第一个字符(例如,c.Language应该等于“ JPY“它仅等于”J“)

我完全错了吗?感谢您的建议

public void AddContactOrder(object sender, EventArgs e)
    {
        IEnumerable OrderFact = new OrdersFactsController().getOrderFacts(base.ModuleId);
        IEnumerator enumerator = OrderFact.GetEnumerator();
        var test = "";
        List<string> lang = new List<string>();
        while (enumerator.MoveNext())
        {

            OrderFact c = (OrderFact)enumerator.Current;
            if (c.Language == "JPY")
            {
                test = "okay";
            }

        }

}

getorderFacts()返回一个IEnumerable,其中T是OrderFact

public class OrderFact
{
    public int ID { get; set; }
    public int ModuleId { get; set; }
    public string ProdCode { get; set; }
    public string Language { get; set; }
    public string Currency { get; set; }
    public string KeyCodes { get; set; }
    public string OrderSourceCode { get; set; }
    public string OfferingCode { get; set; }
    public string JobNumber { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }
}

3 个答案:

答案 0 :(得分:6)

你最好只使用foreach循环:

foreach (var c in new OrdersFactsController().getOrderFacts(base.ModuleID))
{
    if (c.Language == "JPY")
        test = "okay";
}

答案 1 :(得分:5)

您可以使用System.Linq的{​​{3}}:

public void AddContactOrder(object sender, EventArgs e)
{
    var orderFacts = new OrdersFactsController().getOrderFacts(base.ModuleId);
    var test = orderFacts.Any(x => x.Language == "JPY") ? "okay" : "";

}

答案 2 :(得分:3)

    public void AddContactOrder(object sender, EventArgs e)
    {
        IEnumerable<OrderFact> orderFacts = new OrdersFactsController().getOrderFacts(base.ModuleId);
        var test = "";
        if(orderFacts.Any(x => x.Language == "JPY")) test="okay";
    }

LINQ!