LINQ - My second attempt, how could i have done it better
-
I have a bit of code and wanted to swap a nested loop for some LINQ. I have made a test app to develop the LINQ and have it working, but i feel the LINQ could be done better. Any advice most welcome :) the code is below and basically it checks to see if all the items in a list of strings are in another list of myobjects (checking against a string parameter of the object).
class Program
{
static void Main(string[] args)
{
bool areAllListAInListB = true;List<string> listA = new List<string>(); listA.Add("A"); listA.Add("E"); //Comment out next 3 lines to get true returned listA.Add("G"); listA.Add("W"); listA.Add("Z"); List<MyClass> listB = new List<MyClass>(); listB.Add(new MyClass(1, "A", 1)); listB.Add(new MyClass(2, "E", 2)); listB.Add(new MyClass(3, "I", 3)); listB.Add(new MyClass(4, "O", 4)); listB.Add(new MyClass(5, "U", 5)); //Want to replace all this with a LINQ statement /\* foreach (string letter in listA) { bool isVowel = false; foreach (MyClass v in listB) { if (string.Compare(letter, v.A2,true) == 0) { isVowel = true; break; } } if (!isVowel) { areAllListAInListB = false; break; } } \*/ var query =(listA.Count == (from s in listA join i in listB on s equals i.A2 select s).Count())?true:false; areAllListAInListB = query; Console.WriteLine(areAllListAInListB.ToString()); Console.ReadLine(); } class MyClass { int \_a1; string \_a2; int \_a3; public MyClass(int a1, string a2, int a3) { \_a1 = a1; \_a2 = a2; \_a3 = a3; } public int A1 { get { return \_a1; } } public string A2 { get { return \_a2; } } public int A3 { get { ret
-
I have a bit of code and wanted to swap a nested loop for some LINQ. I have made a test app to develop the LINQ and have it working, but i feel the LINQ could be done better. Any advice most welcome :) the code is below and basically it checks to see if all the items in a list of strings are in another list of myobjects (checking against a string parameter of the object).
class Program
{
static void Main(string[] args)
{
bool areAllListAInListB = true;List<string> listA = new List<string>(); listA.Add("A"); listA.Add("E"); //Comment out next 3 lines to get true returned listA.Add("G"); listA.Add("W"); listA.Add("Z"); List<MyClass> listB = new List<MyClass>(); listB.Add(new MyClass(1, "A", 1)); listB.Add(new MyClass(2, "E", 2)); listB.Add(new MyClass(3, "I", 3)); listB.Add(new MyClass(4, "O", 4)); listB.Add(new MyClass(5, "U", 5)); //Want to replace all this with a LINQ statement /\* foreach (string letter in listA) { bool isVowel = false; foreach (MyClass v in listB) { if (string.Compare(letter, v.A2,true) == 0) { isVowel = true; break; } } if (!isVowel) { areAllListAInListB = false; break; } } \*/ var query =(listA.Count == (from s in listA join i in listB on s equals i.A2 select s).Count())?true:false; areAllListAInListB = query; Console.WriteLine(areAllListAInListB.ToString()); Console.ReadLine(); } class MyClass { int \_a1; string \_a2; int \_a3; public MyClass(int a1, string a2, int a3) { \_a1 = a1; \_a2 = a2; \_a3 = a3; } public int A1 { get { return \_a1; } } public string A2 { get { return \_a2; } } public int A3 { get { ret
How about something like this:
// check if all the items in a list of strings are in another list of myobjects
var allElementsNotInListB = from element in listA
let existsInListB = listB.Exists(input => input.A2 == element)
where existsInListB == false
select element;bool allItemsExist = allElementsNotInListB.Count() == 0;
Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango
-
How about something like this:
// check if all the items in a list of strings are in another list of myobjects
var allElementsNotInListB = from element in listA
let existsInListB = listB.Exists(input => input.A2 == element)
where existsInListB == false
select element;bool allItemsExist = allElementsNotInListB.Count() == 0;
Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango