Solved

LINQ question

Posted on 2016-11-08
7
20 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 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
Comment Utility
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
Comment Utility
ignore, the above worked. Thank you for your help.
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Great ! Welcome !!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now