Linq & List.Contains

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy

    Linq & List.Contains

    Hi,

    I'm trying to add a where clause to my query:

    List<stringtype s = new List<string>();
    types.Add( "A" );
    types.Add( "B" );

    query = query.Where( c =types.Contains ( c.Type ) );

    When executed, I get the following exception:
    System.NotSuppo rtedException : Queries with local collections are not
    supported.

    I found a bug filling on Connect that indicates this was fixed for
    RTM.. what's going on?

    Thanks
    Andy
  • Frans Bouma [C# MVP]

    #2
    Re: Linq &amp; List.Contains

    Andy wrote:
    Hi,
    >
    I'm trying to add a where clause to my query:
    >
    List<stringtype s = new List<string>();
    types.Add( "A" );
    types.Add( "B" );
    >
    query = query.Where( c =types.Contains ( c.Type ) );
    >
    When executed, I get the following exception:
    System.NotSuppo rtedException : Queries with local collections are not
    supported.
    >
    I found a bug filling on Connect that indicates this was fixed for
    RTM.. what's going on?
    try:
    query = query.Where(c=> new List<string>(){ "A", "B"}.Contains(c .Type));

    Linq to Sql isn't always smart enough to convert a constant typed
    object into a value-based filter.

    FB


    --
    ------------------------------------------------------------------------
    Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
    LLBLGen Pro website: http://www.llblgen.com
    My .NET blog: http://weblogs.asp.net/fbouma
    Microsoft MVP (C#)
    ------------------------------------------------------------------------

    Comment

    • Andy

      #3
      Re: Linq &amp; List.Contains

      On Apr 1, 10:17 am, "Frans Bouma [C# MVP]"
      <perseus.usenet NOS...@xs4all.n lwrote:
      try:
      query = query.Where(c=> new List<string>(){ "A", "B"}.Contains(c .Type));
      >
      Linq to Sql isn't always smart enough to convert a constant typed
      object into a value-based filter.
      Hmm... well that works. Weird. I should have posted my actual
      code... its like this:

      List<stringtype s = new List<string>();
      if ( conditionA ) {
      types.Add( "A" );
      }

      if ( conditionB ) {
      types.Add( "B" );
      }

      query = query.Where( c =types.Contains ( c.Type ) );

      So unfortunately I can't implement your recommendation.

      Comment

      • =?Utf-8?B?SmFybGF4bGU=?=

        #4
        Re: Linq &amp; List.Contains

        i havent tried it but could you do...

        query = query.Where( c =types.toList() .Contains( c.Type ) );

        "Andy" wrote:
        On Apr 1, 10:17 am, "Frans Bouma [C# MVP]"
        <perseus.usenet NOS...@xs4all.n lwrote:
        try:
        query = query.Where(c=> new List<string>(){ "A", "B"}.Contains(c .Type));

        Linq to Sql isn't always smart enough to convert a constant typed
        object into a value-based filter.
        >
        Hmm... well that works. Weird. I should have posted my actual
        code... its like this:
        >
        List<stringtype s = new List<string>();
        if ( conditionA ) {
        types.Add( "A" );
        }
        >
        if ( conditionB ) {
        types.Add( "B" );
        }
        >
        query = query.Where( c =types.Contains ( c.Type ) );
        >
        So unfortunately I can't implement your recommendation.
        >

        Comment

        • Andy

          #5
          Re: Linq &amp; List.Contains

          On Apr 1, 10:45 am, Jarlaxle <Jarla...@discu ssions.microsof t.com>
          wrote:
          i havent tried it but could you do...
          >
          query = query.Where( c =types.toList() .Contains( c.Type ) );
          Well I tried types.ToArray() , and that didn't work either.

          Comment

          • Frans Bouma [C# MVP]

            #6
            Re: Linq &amp; List.Contains

            Andy wrote:
            On Apr 1, 10:17 am, "Frans Bouma [C# MVP]"
            <perseus.usenet NOS...@xs4all.n lwrote:
            try:
            query = query.Where(c=> new List<string>(){ "A",
            "B"}.Contains(c .Type));

            Linq to Sql isn't always smart enough to convert a constant
            typed object into a value-based filter.
            >
            Hmm... well that works. Weird. I should have posted my actual
            code... its like this:
            >
            List<stringtype s = new List<string>();
            if ( conditionA ) {
            types.Add( "A" );
            }
            >
            if ( conditionB ) {
            types.Add( "B" );
            }
            >
            query = query.Where( c =types.Contains ( c.Type ) );
            >
            So unfortunately I can't implement your recommendation.
            I tried it with our linq provider (now in beta) and your initial query
            works fine. It's also odd that it doesn't work in linq to sql: the new
            List<string... is found by the 'funcletizer' routine in linq to sql's
            engine which converts it into a compiled lambda and executes it. This
            gives... a ConstantExpress ion with the actual object.

            Your initial query has at the spot of the list parameter also a
            ConstantExpress ion which represents the list object. Contains is a
            difficult beast to implement (see my blog for details, I think it was
            episode #12) though in this case, there's no real difference between
            having a constant which is an IList and.... having a constant which is
            an IList.

            Perhaps you should post on the forums.microsof t.com linq forum, as
            the linq to sql team is reading there and perhaps could help you
            further. There are other cases where linq to sql gives up related to
            Contains: it doesn't try to match source element's properties with
            element to check (argument of Contains) in some occasions.

            FB

            --
            ------------------------------------------------------------------------
            Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
            LLBLGen Pro website: http://www.llblgen.com
            My .NET blog: http://weblogs.asp.net/fbouma
            Microsoft MVP (C#)
            ------------------------------------------------------------------------

            Comment

            • Andy

              #7
              Re: Linq &amp; List.Contains

              On Apr 2, 4:04 am, "Frans Bouma [C# MVP]"
              <perseus.usenet NOS...@xs4all.n lwrote:
              I tried it with our linq provider (now in beta) and your initial query
              works fine. It's also odd that it doesn't work in linq to sql: the new
              List<string... is found by the 'funcletizer' routine in linq to sql's
              engine which converts it into a compiled lambda and executes it. This
              gives... a ConstantExpress ion with the actual object.
              >
              Your initial query has at the spot of the list parameter also a
              ConstantExpress ion which represents the list object. Contains is a
              difficult beast to implement (see my blog for details, I think it was
              episode #12) though in this case, there's no real difference between
              having a constant which is an IList and.... having a constant which is
              an IList.
              >
              Perhaps you should post on the forums.microsof t.com linq forum, as
              the linq to sql team is reading there and perhaps could help you
              further. There are other cases where linq to sql gives up related to
              Contains: it doesn't try to match source element's properties with
              element to check (argument of Contains) in some occasions.
              Ok, I'll try posting over there and see if I get any more ideas.

              Thanks
              Andy

              Comment

              • Vinit Modi

                #8
                Re: Linq &amp; List.Contains



                This worked for me
                public int getQty(List<int lstNumbers, string scrip, string client)
                {
                int qty = 0;
                var rows = from table in BsktQtyTbl.AsEn umerable()
                where
                table.Field<str ing>("Client") == client
                &&
                lstNumbers.Cont ains(table.Fiel d<int>("Number" ))

                select new
                {
                qty = table.Field<int >(scrip)
                };
                foreach (var line in rows)
                {
                qty += line.qty;
                }

                return qty;
                }

                *** Sent via Developersdex http://www.developersdex.com ***

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Linq &amp; List.Contains

                  On Sep 16, 8:15 am, Vinit Modi <vinitm...@yaho o.comwrote:
                  This worked for me
                  public int getQty(List<int lstNumbers, string scrip, string client)
                          {
                              int qty = 0;
                              var rows = from table in BsktQtyTbl.AsEn umerable()
                                         where
                                         table.Field<str ing>("Client") == client
                                         &&
                  lstNumbers.Cont ains(table.Fiel d<int>("Number" ))
                  >
                                         select new
                                         {
                                             qty = table.Field<int >(scrip)
                                         };
                              foreach (var line in rows)
                              {
                                  qty += line.qty;
                              }
                  >
                              return qty;
                          }
                  Why are you bothering to create an anonymous type just for the sake of
                  one value? You can also get LINQ to sum the results for you. In other
                  words:

                  public int getQty(List<int lstNumbers, string scrip, string client)
                  {
                  return (from table in BsktQtyTbl.AsEn umerable()
                  where table.Field<str ing>("Client") == client
                  && lstNumbers.Cont ains(table.Fiel d<int>("Number" )))
                  .Sum(table =table.Field<in t>(scrip));
                  }

                  Jon

                  Comment

                  Working...