yield-return

How can I load all child records recursively?

折月煮酒 提交于 2021-02-11 14:24:58
问题 I have this Project class: public class Project { public int Id { get; set; } public int? ParentId { get; set; } public List<Project> ChildProjects { get; set; } // more properties } This is my attempt to load all descendants of any given project: private async Task<List<Project>> LoadDescendantsOf(Project project) { project.ChildProjects = await db.Projects .Where(p => p.ParentId == project.Id) .ToListAsync(); foreach (Project childProject in project.ChildProjects) { yield return

Generating all the substring of a given length using yield

北慕城南 提交于 2020-12-08 10:52:41
问题 I need to generate all the substring of a given length, of a string. For example all the substrings of length 3 of "abcdefg" are: abc bcd cde def efg For this task I wrote this function: public static IEnumerable<string> AllSubstringsLength(string input, int length) { List<string> result = new List<string>(); for (int i = 0; i <= input.Length - length; i++) { result.Add(input.Substring(i, length)); } return result; } that I use like this: foreach(string s in AllSubstringsLength("abcdefg",3))

Generating all the substring of a given length using yield

房东的猫 提交于 2020-12-08 10:52:10
问题 I need to generate all the substring of a given length, of a string. For example all the substrings of length 3 of "abcdefg" are: abc bcd cde def efg For this task I wrote this function: public static IEnumerable<string> AllSubstringsLength(string input, int length) { List<string> result = new List<string>(); for (int i = 0; i <= input.Length - length; i++) { result.Add(input.Substring(i, length)); } return result; } that I use like this: foreach(string s in AllSubstringsLength("abcdefg",3))

Accessing SQLite database in Unity with a coroutine

廉价感情. 提交于 2020-01-15 06:40:09
问题 I've created a menu in Unity which is populated by results from an SQLite DB. However when I create the menu, the whole game freezes for a moment while it queries the DB. To fix this, I'm trying to separate the creation of the menu and the populating of it with data (i.e. the menu will just say "loading" until the query is complete). I have been trying to use a yield-return co-routine to do this but the game is still freezing. Below I have some pseudo-code illustrating what I am doing... void

BinaryWriter problem - “code adds some byte between Write() method”

牧云@^-^@ 提交于 2020-01-14 06:48:38
问题 I am try to do some code using BinaryWriter and Then BinaryReader. When I wanna write I use method Write(). But the problem is that between two lines of Write method there appears a new byte which is in ASCII table in decimal 31 (sometines 24). You can see it on this image: You can see that byte at index 4 (5th byte) is of ASCII decimal value 31. I didnt insert it there . As you can see 1st 4 bytes are reserved for a number (Int32), next are other data (some text mostly - this is not

BinaryWriter problem - “code adds some byte between Write() method”

懵懂的女人 提交于 2020-01-14 06:48:03
问题 I am try to do some code using BinaryWriter and Then BinaryReader. When I wanna write I use method Write(). But the problem is that between two lines of Write method there appears a new byte which is in ASCII table in decimal 31 (sometines 24). You can see it on this image: You can see that byte at index 4 (5th byte) is of ASCII decimal value 31. I didnt insert it there . As you can see 1st 4 bytes are reserved for a number (Int32), next are other data (some text mostly - this is not

Method not called when using yield return

僤鯓⒐⒋嵵緔 提交于 2020-01-11 08:26:14
问题 I'm having a little trouble with a method in which I use yield return this doesn't work... public IEnumerable<MyClass> SomeMethod(int aParam) { foreach(DataRow row in GetClassesFromDB(aParam).Rows) { yield return new MyClass((int)row["Id"], (string)row["SomeString"]); } } The above code never runs, when the call is made to this method it just steps over it. However if I change to... public IEnumerable<MyClass> SomeMethod(int aParam) { IList<MyClass> classes = new List<MyClass>(); foreach