문제

I have a strange situation, I can not find a way why Linq 2 entities query is not returning results.

First query is returning results, but second is not returning results.

summaryData = db.PromoSummary.Where(x => x.Month == state.OldPromoStart.Month).ToList();
summaryData = db.PromoSummary.Where(x => (x.Month == state.OldPromoStart.Month) | (x.Month == state.OldPromoEndMonthMax)).ToList();

if "A" is true, then "A or B" should also be true... (in this case first statement is true, so it should return results regardless of the second statement, right?)

P.S. db is DbContext, PromoSummary is DbSet

Any ideas what I am doing wrong?

EDIT

this PromoSummary DbSet has rows with Month==9, but no rows where Month == 10. I changed | to ||, but the result is the same - first query returns result, but second query returns 0 records...

summaryData = db.PromoSummary.Where(x => x.Month == 9).ToList();
summaryData = db.PromoSummary.Where(x => (x.Month == 9) || (x.Month == 10)).ToList();

EDIT #2

I suppose this is what Andrew suggested... why is that "Is not null" checking? P.S. in database Month property is called SummaryMonth (defined with Fluent api) and one more thing - this property is of type byte

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[CustomerId] AS [CustomerId], 
[Extent1].[SummaryYear] AS [SummaryYear], 
[Extent1].[SummaryMonth] AS [SummaryMonth], 
[Extent1].[ThemeId] AS [ThemeId], 
[Extent1].[CurrentStateId] AS [CurrentStateId]
FROM [PromotionSummary] AS [Extent1]
WHERE ((9 = ( CAST( [Extent1].[SummaryMonth] AS int))) 
         AND ( CAST( [Extent1].[SummaryMonth] AS int) IS NOT NULL)) 
   OR ((10 = ( CAST( [Extent1].[SummaryMonth] AS int))) 
         AND ( CAST( [Extent1].[SummaryMonth] AS int) IS NOT NULL))
도움이 되었습니까?

해결책

you should use || instead of bitwise or |

For your second update, try using following code:

List<byte> months = new List<byte> {9, 10};

summaryData = db.PromoSummary.Where(x => months.Contains(x.Month)).ToList();

다른 팁

You should use || operator instead of the |:

summaryData = db.PromoSummary.Where(x => (x.Month == state.OldPromoStart.Month) || (x.Month == state.OldPromoEndMonthMax)).ToList();

The | operator is a binary OR, but the || is a logical one.

Because your OR is Binary OR

You should write like:

db.PromoSummary.Where(x => (x.Month == state.OldPromoStart.Month) 
       || (x.Month == state.OldPromoEndMonthMax)).ToList();

EDIT

Considering that this may be SQL driver issue, you may try to do something like this. LINQ is known for defered execution, so you may write:

var first =  db.PromoSummary.Where(x => (x.Month == state.OldPromoStart.Month));
var second=  db.PromoSummary.Where(x => (x.Month == state.OldPromoEndMonthMax));

first.AddRange(second);     
return first.FirstOrDefault();

This should generate SQL you are searching for, so produce also desirable result.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top