473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 46846
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it...
3
by: Brian Mitchell | last post by:
Hello, I have a weird problem with my Visual Basic. The text in list boxes, buttons and message boxes are missing when I run the program. Everything looks fine at design time but somewhere when I...
1
by: Brian Pelton | last post by:
I feel like I'm about to get in over my head, so before I go off the deep end I'm hoping someone can point me in the right direction. I started down the road of using a List<> of an Interface...
1
by: Jeff | last post by:
hey asp.net 2.0 If it's possible then I want to use a "form & list" control on my asp.net 2.0 webpage. In "form & list" I mean "list" displays rows of data, and when the user clicks one of...
0
by: Steve Long | last post by:
Hello. When you start up VS 2005, it displays the list of installed products on it's splash(startup) screen. VS 2003 used to do this also before I installed 2005. Does anyone know why my list of...
2
by: Steve Long | last post by:
Hello. (I posted this yesterday in the .net general group and got no responses. Anybody here?) When you start up VS 2005, it displays the list of installed products on it's splash(startup) screen....
2
by: Jeff | last post by:
..NET 2.0 I'm trying to use List.Contains to determine if an object already exist in the list. This Contains method always return false in my code In my code a method gets a list of names, the...
2
by: ihimitsu | last post by:
Hi friends guide me to sorting java list contains multiple list objects
11
by: mathieu | last post by:
Hi there, I am trying to write something very simple to test if a list contains another one: a = b = but 'a in b' returns False. How do I check that a is indeed contained
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.