简体   繁体   English

C#访问IEnumerable集合

[英]C# accessing IEnumerable collections

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. 我让PetaPoco将查询结果作为IEnumerable返回,每个结果都有一个集合。 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") 到目前为止,我能够迭代Enumerable,并且看起来能够根据我的代码片段访问一个对象,但是当我在调试中查看c.Language时,它只是字符串的第一个字符(例如,c.Language应该等于“ JPY“它仅等于”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 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; }
}

You're better off just using a foreach loop: 你最好只使用foreach循环:

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

You could use System.Linq 's Any extension method : 您可以使用System.LinqAny扩展方法

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

}
    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! LINQ!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IEnumerable集合的串联C# - Concatenation of IEnumerable collections C# C#动态并使用IEnumerable集合 - C# dynamic and working with IEnumerable collections C#动态访问子级为IEnumerable - c# dynamic accessing child as IEnumerable 在C#中,在System.Collections.Generic.IEnumerable`1中IEnumerable之后1的含义是什么 - In C# what is the meaning of 1 after IEnumerable in System.Collections.Generic.IEnumerable`1 C#JSON错误=&gt;无法将JSON对象反序列化为类型&#39;System.Collections.Generic.IEnumerable`1 - C# JSON error => Cannot deserialize JSON object into type 'System.Collections.Generic.IEnumerable`1 C#,无法从“ System.Collections.Generic.IEnumerable”转换为“ string []” - C#, cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]' C# 无法将类型“XXXX.Classes.Idophalen”隐式转换为“System.Collections.IEnumerable” - C# Cannot implicitly convert type 'XXXX.Classes.Idophalen' to 'System.Collections.IEnumerable' 如何使用LINQ C#从System.Collections.IEnumerable获取属性列表 - How to get List of Properities from System.Collections.IEnumerable using LINQ C# C# 无法将“字符串”转换为“System.Collections.Generic.IEnumerable”<string></string> - C# cannot convert 'string' to 'System.Collections.Generic.IEnumerable<string> C#:错误-无法将类型&#39;int&#39;隐式转换为&#39;System.Collections.Generic.IEnumerable <double> “ - C#: Error-Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<double>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM