1
votes

i have used below linq query code but it returns "Sequence contains no elements" , i am sure there is one item that should be return.

the code is like below:

tblDocTranstoCon doctranstocon =_DataContext.tblDocTranstoCons
                       .Single(dtcon => (dtcon.Docid == _DocID)
                                     && (dtcon.Transid==e.TransmittoconID)
                                     && (dtcon.Transid==e.TransID));
2
Any chance you have the comparisons wrong? You're comparing dtcon.Transid to both e.TransmittoconID and e.TransID - I'm guessing the second one is right, but perhaps the first one is referring to the wrong field on dtcon? - Jonathan Rupp
why condition( dtcon.Transid ==) is two times? - andy
thanks it seems problem is "why condition( dtcon.Transid ==) is two times?" - masoud

2 Answers

2
votes

You should use SingleOrDefault if there is no item returned.

tblDocTranstoCon doctranstocon =_DataContext.tblDocTranstoCons
   .SingleOrDefault(dtcon => (dtcon.Docid == _DocID)
                   && (dtcon.Transid == e.TransmittoconID)
                   && (dtcon.Transid == e.TransID));
0
votes

To solve the problem replace the method Single() call with SingleOrDefault() method. The method SingleOrDefault() returns a null value if there are no source records that matches to the filtering criteria. see here