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 with ref parameter

LINQ with ref parameter

Scheduled Pinned Locked Moved C#
csharplinq
3 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.
  • S Offline
    S Offline
    sunsher
    wrote on last edited by
    #1

    Hi, I'd like to know if it's possible to get a collection of something with LINQ with the 'where' clause equal to a function that has a ref parameter.

    var objCol = from id in ids
    where id.GetValue(2, ref strValue) == "6"
    select id;

    The result should be based on the ref value. Thanks

    P Richard DeemingR 2 Replies Last reply
    0
    • S sunsher

      Hi, I'd like to know if it's possible to get a collection of something with LINQ with the 'where' clause equal to a function that has a ref parameter.

      var objCol = from id in ids
      where id.GetValue(2, ref strValue) == "6"
      select id;

      The result should be based on the ref value. Thanks

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      It certainly is possible. Here's a little sample for you to try:

      public class LinqTester
      {
      public void Test()
      {
      LinqCollection collection = new LinqCollection(
      List<LinqItem> values = collection.Where(x =>
      {
      string value = string.Empty;
      x.GetValue(6, ref value);
      return value == "6";
      }).ToList();

      // Should have one value here - you can do something with it now.
      

      }
      }

      public class LinqCollection : List<LinqItem>
      {
      public LinqCollection()
      {
      for (int i = 0; i < 10; i++)
      {
      this.Add(new LinqItem { Id = i });
      }
      }
      }

      public class LinqItem
      {
      public int Id { get; set; }
      public void GetValue(int item, ref string value)
      {
      value = Id.ToString();
      }
      }

      This space for rent

      1 Reply Last reply
      0
      • S sunsher

        Hi, I'd like to know if it's possible to get a collection of something with LINQ with the 'where' clause equal to a function that has a ref parameter.

        var objCol = from id in ids
        where id.GetValue(2, ref strValue) == "6"
        select id;

        The result should be based on the ref value. Thanks

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Well, you can, as Pete showed you. But it's not really a good idea, and probably won't work as you want it to. It would be better to change the method so that it doesn't use a ref or out parameter. If you can't change it, then write a wrapper method. You can either return a specific type to encapsulate the two returned values, or use a Tuple[^]. In C#7, there will even be built-in language support for tuples[^], which will make this much easier.

        static class YourExtensions
        {
        // NB: In this case, a specific type would be better, since it's not
        // immediately obvious which tuple item represents which value.

        public static Tuple<string, string> GetValue(this YourType id, int theParameter)
        {
            string strValue = null;
            string result = id.GetValue(theParameter, ref strValue);
            return Tuple.Create(result, strValue);
        }
        

        }
        ...
        var objCol = from id in ids
        let value = id.GetValue(2)
        where value.Item1 == "6"
        select new { id, strValue = value.Item2 };


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        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