Reputation: 973
I am having lambda expression where I am getting record when all the values are equal in input object and db object
var matchingrecord = (from data in dbContext.TableA
where data.Id == inputDto.Id &&
data.AddTypeId == inputDto.AddressTypeId &&
inputDto.AddressLineOne == data.AddressLineOne) &&
(inputDto.AddressLineTwo == (data.AddressLineTwo ?? string.Empty)) &&
(inputDto.State ==null||(inputDto.State.stateId == data.State.StateId)) &&
(inputDto.City == data.City) &&
(inputDto.ZipCd == data.ZipCd)
select data).FirstOrDefault();
I need to include in where clause another field check(zipcodeExtn) only when zipcodeExtn is not null.
(inputDto.City == data.City) &&
(inputDto.ZipCd == data.ZipCd) &&
(data.zipcodeExtn!=null && inputDto.zipcodeExtn==data.zipcodeExtn) // This should be added only when zipcodeExtn is not null
Please let me know if its possible to write one statement instead of adding if condition. and let me know if there is any alternate to this logic for zipcdExtn
Upvotes: 0
Views: 43
Reputation: 11037
Don't think about it in terms of "add statement if condition". Instead, think about building a boolean expression that is true in the cases you want to accept. Do you want that expression to evaluate to true
if inputDto.zipcodeExtn == null
, regardless of anything else? The boolean Or operator does that: inputDto.zipcodeExtn == null || (...expression)
.
Upvotes: 1