我正在学习C#中的迭代器概念,并且正在试验代码,采用简单的问题并尝试以不同的方式实现.我试图在列表中显示所有术语,因为我正在尝试不同的方法来获得结果.在下面的代码中,我使用了两个类ListIterator和ImplementList.
在ListIterator类中:我定义了一个HashSet,它使用IEnumerator来存储值.这里GetEnumerator()方法返回列表中的值.GetEnumerator在ImplementList类(其他类)中实现.最后,列表显示在控制台中.
public class ListIterator
{
public void DisplayList()
{
HashSet<int> myhashSet = new HashSet<int> { 30, 4, 27, 35, 96, 34};
IEnumerator<int> IE = myhashSet.GetEnumerator();
while (IE.MoveNext())
{
int x = IE.Current;
Console.Write("{0} ", x);
}
Console.WriteLine();
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
在ImplementList类中:定义了GetEnumerator(),它使用yield return x返回列表.
public class ImplementList : IList<int>
{
private List<int> Mylist = new List<int>();
public ImplementList() { }
public void Add(int item)
{
Mylist.Add(item);
}
public IEnumerator<int> GetEnumerator()
{
foreach (int x in Mylist)
yield return x;
} …Run Code Online (Sandbox Code Playgroud) 我有一个方法从缓存中返回一个对象,我用它来填充系统中的选择列表.
var r = CacheHelper.GetCacheItem(...);
return new SelectList((IEnumerable)r, "Id", "Name");
Run Code Online (Sandbox Code Playgroud)
r 是类型的 System.Collections.Generic.List<<>f__AnonymousType39<string,int,string>>
通过使用IEnumerable enumerable = (IEnumerable)r;我看到
enumerable如下所示:
[0]: { Name = "Lost", Id = 1, Area = null }
[1]: { Name = "Found", Id = 2, Area = null }
[2]: { Name = "Stock Adjustment", Id = 3, Area = null }
...
Run Code Online (Sandbox Code Playgroud)
我希望能够使用LINQ来查询结果集以返回子集,同时将完整列表保留在内存中.就像是:
var s = enumerable.Where(x => x.Area == "myarea");
Run Code Online (Sandbox Code Playgroud)
类似的SO问题的答案建议使用ToList(),但我不能在enumerable没有收到System.Collections.IEnumerable' does not contain a definition for 'ToList'..错误消息的情况下调用任何LINQ方法.(我在代码中的其他位置使用ToList(),因此我有一个有效的引用 …
我有一个班级人物:
public class Person
{
public string Name {get; set;}
public string Surname {get; set;}
public uint Age {get; set;}
public Person(string name, string surname, uint age)
{
Name= name;
Surname= surname;
Age= age;
}
public Person() : this("default","default",25) {}
}
Run Code Online (Sandbox Code Playgroud)
还有一个包含Person对象集合的类:
public class People : IEnumerable<Person>
{
private List<Person> people= new List<Person> ();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
people.GetEnumerator();
}
public IEnumerator<Person> GetEnumerator()
{
return GetEnumerator();
}
public void Add(Person p)
{
people.Add(p);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想尝试创建一个LINQ连接查询,它尝试对具有相同Name属性的人进行分组,创建一个包含该名称的新匿名对象,以及两个姓氏(Surname1,Surname2):
People ppl1 = new People() …Run Code Online (Sandbox Code Playgroud) 如何有效投IEnumerable入ObservableCollection,而无需创建一个新ObservableCollection的WPF应用程序?
我看到的所有示例都需要创建一个新的ObservableCollection,但我不希望这样.
我有一个期望的方法,IEnumerable<string>你可以在这里看到:
public static string FromDecimalAscii(IEnumerable<string> input)
{
return new string(input.Select(s => (char)int.Parse(s)).ToArray());
}
Run Code Online (Sandbox Code Playgroud)
但每次我的IEnumerable的最后一条记录都是空的,所以我在这一行得到一个错误,因为:
return new string(input.Select(s => (char)int.Parse(s)).ToArray());
Run Code Online (Sandbox Code Playgroud)
所以我必须从我的IEnumerable.删除该项目.
错误:Input string was not in a correct format
任何想法将不胜感激.
最好的祝福
所以,我需要调用第三方方法,它有这样的签名
ThirdPartyMethod<T>(IEnumerable<T> items)
我的问题是我在编译时不知道我的对象的类型.
在编译时我有这个
IEnumerable myObject;
Type typeOfEnumerableIHave;
Run Code Online (Sandbox Code Playgroud)
Sooo ..反思可以帮我解决这个问题吗?
为简单起见,假装我有这样的方法
void DoWork(IEnumerable items, Type type)
{
//In here I have to call ThirdPartyMethod<T>(IEnumerable<T> items);
}
Run Code Online (Sandbox Code Playgroud) 我有两个 IEnumerable<string>
IEnumerable<String> titleCollection = htmlDoc.DocumentNode.SelectNodes("//h2[@class=
'entry-title']/a").Select(x => x.InnerHtml);
IEnumerable<String> allImages = htmlDoc.DocumentNode.SelectNodes("//div[@class=
'entry-thumbnail hover-thumb']/a/img").Select(x => x.Attributes["src"].Value);
Run Code Online (Sandbox Code Playgroud)
是否有可能将它们合并为单一IEnumerable<HTMLELEmsnts>.确定会有并发症,但有没有办法.
class HTMLElements
{
//private string _articeName;
public string ArticleName { get; set; }
public string ImageURL { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我正在构建一个简单的MVC应用程序来执行CRUD应用程序.我有两个简单的模型类.
public class MoniModel
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
public class MoniGridModel
{
public IEnumerable<MoniModel> MoniDetails { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我试图执行以下操作:
public ActionResult MoniDetails()
{
MoniModel mim = new MoniModel();
MoniGridModel migm = new MoniGridModel();
mim.CategoryId = 1;
mim.CategoryName = "a";
mim.ProductId = 1;
mim.ProductName = "b";
migm.MoniDetails.ToList().Add(mim);
return View(migm);
}
Run Code Online (Sandbox Code Playgroud)
当migm.MoniDetails.ToList().Add(mim); 执行时,它会给出上述错误.我无法弄清楚为什么会这样.我正在为mim对象的每个成员赋值.下面是错误细节,但不确定这是否有帮助.

我在IEnumerable下面进行迭代.
IEnumerable<string> readFile = ... ;
int lineNumber = 1;
foreach (string readLine in readFile)
{
...
lineNumber++;
}
Run Code Online (Sandbox Code Playgroud)
但在循环中我必须检查下一行.我用ElementAt它:
foreach (string readLine in readFile)
{
...
if (readFile.ElementAt(lineNumber+1) == ...)
{
// Do something
}
lineNumber++;
}
Run Code Online (Sandbox Code Playgroud)
当我使用上面的代码时ElementAt,我得到了Possible multiple enumeration of IEnumerable
你能解释一下吗?或者我可以使用另一种解决方案吗?我必须使用IEnumerable,因为我正在使用大文件
我必须实现扩展方法
public static IEnumerable<TResult> MyMerge<T1, T2, TResult>(this IEnumerable<T1> s1, IEnumerable<T2> s2, Func<T1, T2, TResult> f)
Run Code Online (Sandbox Code Playgroud)
返回序列f(x1,y1),f(x2,y2)...其中所有xn是s1的元素,所有yn是s2的元素.
我的问题是这个序列必须具有相同长度的最短序列,并且s1和s2都可以是无限的.如果他们不这样我就能做到
var res = new List<TResult>();
for (var i = 0; i<Math.Min(s1.Count(), s2.Count()) ; i++)
{
res.Add(f(s1.ToArray()[i], s2.ToArray()[i]));
}
return res;
Run Code Online (Sandbox Code Playgroud)
但是当一个序列比另一个序列长时,这也会破坏.我该如何解决这个问题?