Solved

LINQ question

Posted on 2016-11-08
7
66 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 500 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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