?
Solved

LINQ question

Posted on 2016-11-08
7
Medium Priority
?
70 Views
Last Modified: 2016-11-08
Dear Experts,
How do I can combine the results in this LINQ query?

c.field == 1 below generates results and c.field == 3 generates results but how can I show the list of both results together. This below generates no results.  I guess one is cancelling out the other.

var today = DateTime.Now.Date;
var tomorrow = today.AddDays(1);

var query = from c in databasetable
						  where c.date > today && c.date < tomorrow
						  && c.field == 1 
						  && c.field == 3
						  select c;
						  
query.Dump();

Open in new window

0
Comment
Question by:sunny-j
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41878272
Try..

var today = DateTime.Now.Date;
var tomorrow = today.AddDays(1);

var query = from c in databasetable
						  where c.date > today && c.date < tomorrow
						  && ( c.field == 1 
						  || c.field == 3 )
						  select c;
						  
query.Dump();

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41878280
>>I guess one is cancelling out the other.

Yes.  The && means and and c.field can't be equal to 1 and 3 and the same time.  The || (previous comment by the other expert) means or.  So it matches c.field to either 1 or 3
0
 

Author Comment

by:sunny-j
ID: 41878287
Thank you for your comments. I do want results from c.field == 1 and c.field == 3 (these are states in a picklist) so do I need to approach this differently, maybe by grouping the results.  The II will give me one or the other so doesn't solve my issue.  Any further help must appreciated.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41878298
On column c cannot have 2 values at the same time. It can either have 1 or 3 at the same time.  You need to create 2 queries and then perform your logic.

var today = DateTime.Now.Date;
var tomorrow = today.AddDays(1);

var query = from c in databasetable
						  where c.date > today && c.date < tomorrow
						  && ( c.field == 1 )						 
						  select c;

var query1 = from c in databasetable
						  where c.date > today && c.date < tomorrow
						  && ( c.field == 3 )						 
						  select c;
						  
query.Dump();
query1.Dump(); 

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41878301
Using the || operator will give from both c.field=1 and c.field=3.  Which is what you want you say.
1
 

Author Comment

by:sunny-j
ID: 41878307
ignore, the above worked. Thank you for your help.
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41878311
Great ! Welcome !!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question