473,320 Members | 2,073 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,320 software developers and data experts.

Using an "and" in LINQ

55
Is it possible to use an "and" in LINQ i.e:

var londonCustomers = from customer in Customers
where customer.City == "London"

and customer.CreditCardType="Visa"
select customer;
if yes/no how do I achieve this?
Sep 3 '10 #1

✓ answered by balabaster

Expand|Select|Wrap|Line Numbers
  1. var londonCustomers = from c in Customers
  2.                       where (c.City.Equals("London") && c.CreditCardType.Equals("Visa"))
  3.                       select c;
If you only needed certain parts, then you'd use the select to filter the relevant fields. For example:

Expand|Select|Wrap|Line Numbers
  1. from c in Customers
  2. where customer.City.Equals("London")
  3. select new { Name = c.Name, PostCode = c.PostCode }
You could also use method syntax instead of query syntax:

Expand|Select|Wrap|Line Numbers
  1. var londonCustomers = Customers.Where(c => c.City.Equals("London") && c.CreditCardType.Equals("Visa"));
Of course, you'd need to understand lambda expressions to be able to do that.

You could also do this without LINQ:

Expand|Select|Wrap|Line Numbers
  1. var londonCustomers.Find(c => c.City.Equals("London") && c.CreditCardType.Equals("Visa"));
The beauty of the non-LINQ syntax is that it works in .NET 2.0 codebases

When you're thinking about LINQ, it does have some similarities to SQL, but it's not the same. You need to think about it slightly differently. Sure you've got your "from c in Customers", but your "where" isn't structured the same way as in SQL:

The where clause is basically the same as your .NET 2.0 find. Your query is iterating every object in your collection, so you treat each object exactly as you would if you were writing code relating to it - think of an if statement:

Expand|Select|Wrap|Line Numbers
  1. if (c.Location.Equals("London") && (c.PostCode.StartsWith("EC1") || c.PostCode.StartsWith("SW11")) { ... }
Your where clause is exactly the same...

3 1253
balabaster
797 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. var londonCustomers = from c in Customers
  2.                       where (c.City.Equals("London") && c.CreditCardType.Equals("Visa"))
  3.                       select c;
If you only needed certain parts, then you'd use the select to filter the relevant fields. For example:

Expand|Select|Wrap|Line Numbers
  1. from c in Customers
  2. where customer.City.Equals("London")
  3. select new { Name = c.Name, PostCode = c.PostCode }
You could also use method syntax instead of query syntax:

Expand|Select|Wrap|Line Numbers
  1. var londonCustomers = Customers.Where(c => c.City.Equals("London") && c.CreditCardType.Equals("Visa"));
Of course, you'd need to understand lambda expressions to be able to do that.

You could also do this without LINQ:

Expand|Select|Wrap|Line Numbers
  1. var londonCustomers.Find(c => c.City.Equals("London") && c.CreditCardType.Equals("Visa"));
The beauty of the non-LINQ syntax is that it works in .NET 2.0 codebases

When you're thinking about LINQ, it does have some similarities to SQL, but it's not the same. You need to think about it slightly differently. Sure you've got your "from c in Customers", but your "where" isn't structured the same way as in SQL:

The where clause is basically the same as your .NET 2.0 find. Your query is iterating every object in your collection, so you treat each object exactly as you would if you were writing code relating to it - think of an if statement:

Expand|Select|Wrap|Line Numbers
  1. if (c.Location.Equals("London") && (c.PostCode.StartsWith("EC1") || c.PostCode.StartsWith("SW11")) { ... }
Your where clause is exactly the same...
Sep 3 '10 #2
Frinavale
9,735 Expert Mod 8TB
Balabaster: You could also do this without LINQ:

Expand|Select|Wrap|Line Numbers
  1. var londonCustomers.Find(c => c.City.Equals("London") && c.CreditCardType.Equals("Visa"));
The beauty of the non-LINQ syntax is that it works in .NET 2.0 codebases
I'm a bit confused.
The non-linq solution makes use of lambda expressions but I didn't think they were available until 3.5?
Could you clarify what you mean?

-Frinny
Sep 7 '10 #3
balabaster
797 Expert 512MB
You know what, you may be right about the lambda expressions, they may not have been available until 3.5. To be honest, I'm not entirely sure. However, the lambda expressions will still work with the non-LINQ code in 3.5 for sure, it's possible that they won't work in 2.0.
Sep 7 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jeff Sandler | last post by:
I have a web page. It uses JavaScript to test the user's input before sending it to the server. It frequently tests using isNaN() with some very interesting results. The statements in question...
9
by: rnn98 | last post by:
hi, my multithread application, running under solaris box, is crashing eventually. I tried to spot and substitute functions not "thread safe", but I guess my search wasn't good enough. I have put...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
0
by: The Last Danish Pastry | last post by:
A couple of days ago there was a thread "Returning the machine epsilon". The responses in that thread suggested using Double.Epsilon or Single.Epsilon. Since both these seem to return zero I...
11
by: ajikoe | last post by:
Hello, I used Visual C# Standard Edition. I want to comment my program using xml commentary method, I don't know why if I use value and example tag, it is not working / showed in the html...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
0
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and...
11
by: Alexander Vasilevsky | last post by:
Linq "into" and "let" equally??? http://www.alvas.net - Audio tools for C# and VB.Net developers
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
13
by: Kurda Yon | last post by:
Hi, I found one example which defines the addition of two vectors as a method of a class. It looks like that: class Vector: def __add__(self, other): data = for j in range(len(self.data)):...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.