C#访问IEnumerable集合
本文关键字:集合 IEnumerable 访问 | 更新日期: 2023-09-27 17:50:38
我对处理集合还很陌生,所以请耐心听我说,我的行话甚至可能不准确。
我让PetaPoco将查询结果作为IEnumerable返回,每个结果一个集合。我想评估集合,以便从每个集合中的特定字段中获得特定字符串。到目前为止,我能够迭代Enumerable,并且似乎能够按照下面的代码片段访问对象,但当我在调试中查看c.语言时,它只是字符串的第一个字符(例如,c.语言应该等于"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; }
}
最好只使用foreach循环:
foreach (var c in new OrdersFactsController().getOrderFacts(base.ModuleID))
{
if (c.Language == "JPY")
test = "okay";
}
您可以使用System.Linq
的Any
扩展方法:
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";
}
林克!