469,286 Members | 2,498 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

Linq & List.Contains

Hi,

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

List<stringtypes = 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.NotSupportedException : 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
Apr 1 '08 #1
8 46543
Andy wrote:
Hi,

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

List<stringtypes = 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.NotSupportedException : 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#)
------------------------------------------------------------------------
Apr 1 '08 #2
On Apr 1, 10:17 am, "Frans Bouma [C# MVP]"
<perseus.usenetNOS...@xs4all.nlwrote:
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<stringtypes = 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.
Apr 1 '08 #3
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.usenetNOS...@xs4all.nlwrote:
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<stringtypes = 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.
Apr 1 '08 #4
On Apr 1, 10:45 am, Jarlaxle <Jarla...@discussions.microsoft.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.
Apr 1 '08 #5
Andy wrote:
On Apr 1, 10:17 am, "Frans Bouma [C# MVP]"
<perseus.usenetNOS...@xs4all.nlwrote:
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<stringtypes = 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 ConstantExpression with the actual object.

Your initial query has at the spot of the list parameter also a
ConstantExpression 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.microsoft.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#)
------------------------------------------------------------------------
Apr 2 '08 #6
On Apr 2, 4:04 am, "Frans Bouma [C# MVP]"
<perseus.usenetNOS...@xs4all.nlwrote:
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 ConstantExpression with the actual object.

Your initial query has at the spot of the list parameter also a
ConstantExpression 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.microsoft.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
Apr 2 '08 #7


This worked for me
public int getQty(List<intlstNumbers, string scrip, string client)
{
int qty = 0;
var rows = from table in BsktQtyTbl.AsEnumerable()
where
table.Field<string>("Client") == client
&&
lstNumbers.Contains(table.Field<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 ***
Sep 16 '08 #8
On Sep 16, 8:15*am, Vinit Modi <vinitm...@yahoo.comwrote:
This worked for me
public int getQty(List<intlstNumbers, string scrip, string client)
* * * * {
* * * * * * int qty = 0;
* * * * * * var rows = from table in BsktQtyTbl.AsEnumerable()
* * * * * * * * * * * *where
* * * * * * * * * * * *table.Field<string>("Client") == client
* * * * * * * * * * * *&&
lstNumbers.Contains(table.Field<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<intlstNumbers, string scrip, string client)
{
return (from table in BsktQtyTbl.AsEnumerable()
where table.Field<string>("Client") == client
&& lstNumbers.Contains(table.Field<int>("Number")))
.Sum(table =table.Field<int>(scrip));
}

Jon
Sep 16 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Yomanium Yoth Taripoät II | last post: by
3 posts views Thread by Brian Mitchell | last post: by
1 post views Thread by Brian Pelton | last post: by
1 post views Thread by Jeff | last post: by
reply views Thread by Steve Long | last post: by
2 posts views Thread by Steve Long | last post: by
2 posts views Thread by Jeff | last post: by
11 posts views Thread by mathieu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.