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. C#
  4. Linq - Conditions for two categories in parallel

Linq - Conditions for two categories in parallel

Scheduled Pinned Locked Moved C#
databasecsharplinqquestion
8 Posts 3 Posters 0 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.
  • R Offline
    R Offline
    Ronenb
    wrote on last edited by
    #1

    Hi
    I’ve a question regarding linq,
    I’ve a DB contains main items (Product) and internal item(properties)

    Class Product
    {
    String name
    List<properties> propList { get; set; }
    }

    Class properties
    {
    String name
    String value
    }

    I would like to know if it possible to create a query that apply on two or more categories on the same internal item at one time
    Such as, if the product is “car” and if the car year is 2012 and color is “Red” I would like to get this record

    List< Product > DB;

    I know the syntax below is wrong, but this is the idea I wish to do
    Enumerable< Product > itemQuery = from item in DB where item.name=="car"
    where
    (
    from prop in item.propList
    where (prop.name.Equals("Color") && prop.value == "Red") && (prop.name.Equals("Year") && prop.value == "2012")
    select field
    ).Any()
    select item;

    W L 3 Replies Last reply
    0
    • R Ronenb

      Hi
      I’ve a question regarding linq,
      I’ve a DB contains main items (Product) and internal item(properties)

      Class Product
      {
      String name
      List<properties> propList { get; set; }
      }

      Class properties
      {
      String name
      String value
      }

      I would like to know if it possible to create a query that apply on two or more categories on the same internal item at one time
      Such as, if the product is “car” and if the car year is 2012 and color is “Red” I would like to get this record

      List< Product > DB;

      I know the syntax below is wrong, but this is the idea I wish to do
      Enumerable< Product > itemQuery = from item in DB where item.name=="car"
      where
      (
      from prop in item.propList
      where (prop.name.Equals("Color") && prop.value == "Red") && (prop.name.Equals("Year") && prop.value == "2012")
      select field
      ).Any()
      select item;

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      First off, this should have been posted in the LinQ forum LinQ Forum[^], but as you posted here, I will answer here. You can use the Contains keyword to find if the item's property list contains the required property, something like this:-

      var itemQuery = DB.Where((i) => i.name == "car" &&
      i.propList.Contains((from prop in i.propList where prop.name == "Colour " && prop.value == "Red" select prop).FirstOrDefault()) &&
      i.propList.Contains((from prop in i.propList where prop.name == "Year" && prop.value == "2012" select prop).FirstOrDefault()));

      although this looks ugly and seems to be a bit of a hack it will return the correct item. Hope this helps

      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

      1 Reply Last reply
      0
      • R Ronenb

        Hi
        I’ve a question regarding linq,
        I’ve a DB contains main items (Product) and internal item(properties)

        Class Product
        {
        String name
        List<properties> propList { get; set; }
        }

        Class properties
        {
        String name
        String value
        }

        I would like to know if it possible to create a query that apply on two or more categories on the same internal item at one time
        Such as, if the product is “car” and if the car year is 2012 and color is “Red” I would like to get this record

        List< Product > DB;

        I know the syntax below is wrong, but this is the idea I wish to do
        Enumerable< Product > itemQuery = from item in DB where item.name=="car"
        where
        (
        from prop in item.propList
        where (prop.name.Equals("Color") && prop.value == "Red") && (prop.name.Equals("Year") && prop.value == "2012")
        select field
        ).Any()
        select item;

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Yes it is quite simple in one line.

        var result = products.Where(item => item.Properties.Any(prop => prop.Name == name && prop.Value == value));

        Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

        1 Reply Last reply
        0
        • R Ronenb

          Hi
          I’ve a question regarding linq,
          I’ve a DB contains main items (Product) and internal item(properties)

          Class Product
          {
          String name
          List<properties> propList { get; set; }
          }

          Class properties
          {
          String name
          String value
          }

          I would like to know if it possible to create a query that apply on two or more categories on the same internal item at one time
          Such as, if the product is “car” and if the car year is 2012 and color is “Red” I would like to get this record

          List< Product > DB;

          I know the syntax below is wrong, but this is the idea I wish to do
          Enumerable< Product > itemQuery = from item in DB where item.name=="car"
          where
          (
          from prop in item.propList
          where (prop.name.Equals("Color") && prop.value == "Red") && (prop.name.Equals("Year") && prop.value == "2012")
          select field
          ).Any()
          select item;

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You could warp it in a method allowing you to keep calling in and further reducing the collection size.

          public void test(IEnumerable<Product> products, string name, string value)
          {
          string name1 = "year";
          string name2 = "color";
          string value1 = "2012";
          string value2 = "Red";

                  //Just stream in what you need
                  var result = Filter(Filter(products, name1, value1), name2, value2);
              }
          
              private IEnumerable<Product> Filter(IEnumerable<Product> products, string name, string value)
              {
                  return products.Where(item => item.Properties.Any(prop => prop.Name == name && prop.Value == value));
              }
          

          You could also make the method an "Extension" allowing you to do more of this products.Filter(name1, value1).Filter(name2, value2); To make an extension create a separate file and use this:

          public static class ProductCollectionExtension
          {
          public static IEnumerable<T> Filter<T>(this IEnumerable<T> collection, string name, string value) where T : Product
          {
          return collection.Where(item => item.Properties.Any(prop => prop.Name == name && prop.Value == value));
          }
          }

          Now just include the namespace that you put this extension and any of your Product objects can then use the simplified notation, i.e. collection.Filter(name1, value1).Filter(name2, value2)....

          Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

          R 2 Replies Last reply
          0
          • L Lost User

            You could warp it in a method allowing you to keep calling in and further reducing the collection size.

            public void test(IEnumerable<Product> products, string name, string value)
            {
            string name1 = "year";
            string name2 = "color";
            string value1 = "2012";
            string value2 = "Red";

                    //Just stream in what you need
                    var result = Filter(Filter(products, name1, value1), name2, value2);
                }
            
                private IEnumerable<Product> Filter(IEnumerable<Product> products, string name, string value)
                {
                    return products.Where(item => item.Properties.Any(prop => prop.Name == name && prop.Value == value));
                }
            

            You could also make the method an "Extension" allowing you to do more of this products.Filter(name1, value1).Filter(name2, value2); To make an extension create a separate file and use this:

            public static class ProductCollectionExtension
            {
            public static IEnumerable<T> Filter<T>(this IEnumerable<T> collection, string name, string value) where T : Product
            {
            return collection.Where(item => item.Properties.Any(prop => prop.Name == name && prop.Value == value));
            }
            }

            Now just include the namespace that you put this extension and any of your Product objects can then use the simplified notation, i.e. collection.Filter(name1, value1).Filter(name2, value2)....

            Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

            R Offline
            R Offline
            Ronenb
            wrote on last edited by
            #5

            thank u all, it is a good start to begin with

            1 Reply Last reply
            0
            • L Lost User

              You could warp it in a method allowing you to keep calling in and further reducing the collection size.

              public void test(IEnumerable<Product> products, string name, string value)
              {
              string name1 = "year";
              string name2 = "color";
              string value1 = "2012";
              string value2 = "Red";

                      //Just stream in what you need
                      var result = Filter(Filter(products, name1, value1), name2, value2);
                  }
              
                  private IEnumerable<Product> Filter(IEnumerable<Product> products, string name, string value)
                  {
                      return products.Where(item => item.Properties.Any(prop => prop.Name == name && prop.Value == value));
                  }
              

              You could also make the method an "Extension" allowing you to do more of this products.Filter(name1, value1).Filter(name2, value2); To make an extension create a separate file and use this:

              public static class ProductCollectionExtension
              {
              public static IEnumerable<T> Filter<T>(this IEnumerable<T> collection, string name, string value) where T : Product
              {
              return collection.Where(item => item.Properties.Any(prop => prop.Name == name && prop.Value == value));
              }
              }

              Now just include the namespace that you put this extension and any of your Product objects can then use the simplified notation, i.e. collection.Filter(name1, value1).Filter(name2, value2)....

              Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

              R Offline
              R Offline
              Ronenb
              wrote on last edited by
              #6

              hi I’ve implement your idea and it work great, thank u! I have another question and hope you can assist The use will choose from UI several filter criteria, so the running filter should be dynamic and not hard coded Such as sometimes it could be collection.Filter(name1, value1) Or collection.Filter(name1, value1).Filter(name2, value2) collection.Filter(name1, value1).Filter(name2, value2). Filter(name3, value3) and so on how can I define by code the dynamic filter? Should I use recursive ? int interaction = #UserFilter.count; DoFilter(string name, string value) { While (interaction--) { collection.Filter(UserFilter[interaction].name, UserFilter[interaction].value) DoFilter(UserFilter[interaction].name, UserFilter[interaction].value); } } thanks ronen

              L 1 Reply Last reply
              0
              • R Ronenb

                hi I’ve implement your idea and it work great, thank u! I have another question and hope you can assist The use will choose from UI several filter criteria, so the running filter should be dynamic and not hard coded Such as sometimes it could be collection.Filter(name1, value1) Or collection.Filter(name1, value1).Filter(name2, value2) collection.Filter(name1, value1).Filter(name2, value2). Filter(name3, value3) and so on how can I define by code the dynamic filter? Should I use recursive ? int interaction = #UserFilter.count; DoFilter(string name, string value) { While (interaction--) { collection.Filter(UserFilter[interaction].name, UserFilter[interaction].value) DoFilter(UserFilter[interaction].name, UserFilter[interaction].value); } } thanks ronen

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You are welcome. You could run it as they select the filter. In your code you would keep a filtered collection and keep adjusting it. If they hit "Clear" you would return the filter collection to the raw.

                private IEnumerable<BusinessObject> _filteredCollection; //Initialized to entire set
                private IEnumerable<BusinessObject> _rawData; //Initialized to entire set

                ...

                private void AdjustFilterCollection(string name, string value)
                {
                _filteredCollection = _filteredCollection.Filter(name, value);
                }

                private void ClearFilter()
                {
                _filterCollection = _rawData;
                }

                This method would then get called when the user updates the filter selection. You would then need to handle some UI post back (e.g. if using WPF implement the INotifyPropertyChanged)

                Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                R 1 Reply Last reply
                0
                • L Lost User

                  You are welcome. You could run it as they select the filter. In your code you would keep a filtered collection and keep adjusting it. If they hit "Clear" you would return the filter collection to the raw.

                  private IEnumerable<BusinessObject> _filteredCollection; //Initialized to entire set
                  private IEnumerable<BusinessObject> _rawData; //Initialized to entire set

                  ...

                  private void AdjustFilterCollection(string name, string value)
                  {
                  _filteredCollection = _filteredCollection.Filter(name, value);
                  }

                  private void ClearFilter()
                  {
                  _filterCollection = _rawData;
                  }

                  This method would then get called when the user updates the filter selection. You would then need to handle some UI post back (e.g. if using WPF implement the INotifyPropertyChanged)

                  Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                  R Offline
                  R Offline
                  Ronenb
                  wrote on last edited by
                  #8

                  ok got it ronen

                  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