-
-
Save anonymous/6942121 to your computer and use it in GitHub Desktop.
Loop Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Data | |
{ | |
public string Name { get; set; } | |
public int ID { get; set; } | |
public int ID2 { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataHolder | |
{ | |
public Data x; | |
public Data y; | |
public override int GetHashCode() | |
{ | |
return x.ID + y.ID; | |
} | |
public override bool Equals(object obj) | |
{ | |
var v = obj as DataHolder; | |
return v.x.ID == x.ID && v.y.ID == y.ID; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var itemx = ( | |
from x in list | |
join y in list2 on x.ID equals y.ID | |
select new DataHolder { x = x, y = y } | |
).ToList(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var list = new List<Data>(); | |
var list2 = new List<Data>(); | |
var random = new Random(); | |
for (var i = 0; i < 1000; i++) | |
{ | |
//var rand = random.Next(2, 500); | |
//var rand2 = random.Next(2, 90); | |
//list.Add(new Data { ID = rand, ID2 = rand2, Name = "Name" + rand }); | |
list.Add(new Data { ID = 1, ID2 = 1, Name = "Name" + 1 }); | |
} | |
for (var i = 0; i < 200; i++) | |
{ | |
//var rand = random.Next(2, 500); | |
//var rand2 = random.Next(2, 90); | |
//list.Add(new Data { ID = rand, ID2 = rand2, Name = "Name" + rand }); | |
list.Add(new Data { ID = 2, ID2 = 2, Name = "Name" + 2 }); | |
} | |
for (var i = 0; i < 1000; i++) | |
{ | |
//var rand = random.Next(2, 500); | |
//var rand2 = random.Next(2, 90); | |
//list.Add(new Data { ID = rand, ID2 = rand2, Name = "Name" + rand }); | |
list2.Add(new Data { ID = 1, ID2 = 1, Name = "Name" + 1 }); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static List<JoinedQuery<Data>> GetList(List<Data> list, List<Data> list2) | |
{ | |
var listCount = list.Count; | |
var list2Count = list2.Count; | |
var result = new List<JoinedQuery<Data>>(); | |
for (var i = 0; i < listCount; i++) | |
{ | |
var item = list[i]; | |
for (var j = 0; j < list2Count; j++) | |
{ | |
var item2 = list2[j]; | |
if (item2.ID == item.ID) | |
{ | |
result.Add(new JoinedQuery<Data> { First = item, | |
Others = new Dictionary<string, object>() { {"list2", item2} } }); | |
break; | |
} | |
} | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static List<JoinedQuery<Data>> GetList2(List<Data> list, List<Data> list2) | |
{ | |
var listCount = list.Count; | |
var list2Count = list2.Count; | |
var result = new List<JoinedQuery<Data>>(); | |
var dictList2 = new Dictionary<int, Data>(); | |
var key = 0; | |
for (var i = 0; i < list2Count; i++) | |
{ | |
var item = list2[i]; | |
key = int.MaxValue; | |
key = (-1521134295 * key) + item.ID; | |
dictList2[key] = item; | |
} | |
for (var i = 0; i < listCount; i++) | |
{ | |
var item = list[i]; | |
key = int.MaxValue; | |
key = (-1521134295 * key) + item.ID; | |
if (dictList2.ContainsKey(key)) | |
{ | |
result.Add(new JoinedQuery<Data> { | |
First = item, | |
Others = new Dictionary<string, object>() { { "list2", dictList2[key] } } | |
}); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment