Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. LINQ - My second attempt, how could i have done it better

LINQ - My second attempt, how could i have done it better

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqquestion
3 Posts 2 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mark_w_
    wrote on last edited by
    #1

    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
    
    J 1 Reply Last reply
    0
    • M mark_w_

      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
      
      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • J Judah Gabriel 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

        M Offline
        M Offline
        mark_w_
        wrote on last edited by
        #3

        Thanks for your reply

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups