2

I am fairly new to working with collections so please bear with me my jargon might not even be accurate.

I have PetaPoco returning query results as an IEnumerable, one collection for each result. I want to evaluate the collections to get a specific string from a specific field in each collection. So far I am able to iterate the Enumerable and seeming able to get access an object as per my snippet below but when i view c.Language in debug, it is only the first character of the string (eg where c.Language should equal "JPY" it equals only "J")

am I doing this completely wrong? Thanks for the advice

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() returns an IEnumerable where T is 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; }
}
Mark Hollas
  • 1,107
  • 1
  • 16
  • 44
  • Can you inspect the value of `c` and `c.Language` by debugging and see the expected values in there? – Jon Peterson Jan 04 '13 at 19:01
  • 4
    Side note: do you really want to do iteration by hand? `foreach` or even better LINQ `.Where` would look much shorter/less chance for errors. – Alexei Levenkov Jan 04 '13 at 19:02
  • 1
    @AlexeiLevenkov It looks like he wants `Any`, not `Where`, but yeah. Also, the `IEnumerator` isn't being disposed and he should almost certainly be using `IEnumerable` and not just `IEnumerable`. – Servy Jan 04 '13 at 19:04
  • 2
    Does `getOrderFacts()` return a string by any chance? If so, you're enumerating its `char[]`, and hence the first value is "J" instead of "JPY". – Jon B Jan 04 '13 at 19:04
  • @JonB No, if that was true it'd be crashing when it cast the item to a `OrderFact`. – Servy Jan 04 '13 at 19:12
  • Thanks for all the help..and so quick! @JonB getOrderFacts() returns IEnumerable; – Mark Hollas Jan 04 '13 at 19:14
  • @MarkHollas, I'm asking what the value of `T` is. Although, it's pretty much moot per Servy's point. – Jon B Jan 04 '13 at 19:16
  • moot points aside, as it all equates to learning, T is an object. I added the class to my question as it is too long to add as a comment – Mark Hollas Jan 04 '13 at 19:19
  • 1
    Seems I am mostly to blame here. The code above did work I was just testing against the wrong field. "JPY" is result for c.Currency. c.Language correct result is actually "J". but I did learn a much more efficient way to do things thanks to suggestions and examples using LINQ statements. Thanks again you guys are awesome! – Mark Hollas Jan 04 '13 at 19:34

3 Answers3

6

You're better off just using a foreach loop:

foreach (var c in new OrdersFactsController().getOrderFacts(base.ModuleID))
{
    if (c.Language == "JPY")
        test = "okay";
}
itsme86
  • 19,266
  • 4
  • 41
  • 57
5

You could use System.Linq's Any extension method:

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

}
Dave Hillier
  • 18,105
  • 9
  • 43
  • 87
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!

Nick Bray
  • 1,953
  • 12
  • 18