?
Solved

LINQ question

Posted on 2016-11-08
7
Medium Priority
?
79 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 38

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 45

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 38

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 45

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 38

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

839 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