473,224 Members | 1,383 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,224 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 46828
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.