7

In one of my pages I need to check if the entered information about a customer consists duplicate PAN NO,Email,Mobile No which may have been entered previously.Currently I am trying it using this Linq To SQL statement

    var duplicate = (from dup in dt.Data_Customer_Logs
                     where dup.cPanGirNo == panno 
                           || dup.cEmail == email 
                           || dup.nMobileNo.ToString() == mobno
    select dup).Any(); 

It is working but can anyone help me as to what is the correct method to solve my issue.Also if there are no records found what would be the result. Any suggestions are welcome.

14
  • 1
    what exactly do you mean by "it's not working"? Commented Jun 19, 2012 at 7:25
  • @ErenErsönmez I tried by passing duplicate email id but it didnot execute the "if records found" part. Commented Jun 19, 2012 at 7:28
  • it should work, but there could be issues related to case or leading/trailing spaces Commented Jun 19, 2012 at 7:28
  • @ie Maybe I need to check that ,also what if there are no records found , will it throw exception or just return null. Commented Jun 19, 2012 at 7:29
  • anyway, you should debug the code and find out what is wrong with the where part Commented Jun 19, 2012 at 7:29

2 Answers 2

5
bool duplicateExists = dt.Data_Customer_Logs.Any(x => 
                         x.cPanGirNo == panno 
                      || x.cEmail == email
                      || x.nMobileNo.ToString() == mobno);

This is a tad cleaner if you just want to know if such records exist or not. And I think it will avoid bringing back multiple records to the client side and then doing IEnumerable<T>.Any on the results.

If you need to also get back the records that match the criteria, you can use IQueryable<T>.Where:

var duplicates =  dt.Data_Customer_Logs.Where(x => 
                         x.cPanGirNo == panno 
                      || x.cEmail == email
                      || x.nMobileNo.ToString() == mobno);
if(duplicates.Any())
{
    // use duplicates...
    foreach(var dup in duplicates)
    {
        //use dup.cEmail, dup.nMobileNo, etc.
Sign up to request clarification or add additional context in comments.

8 Comments

I have not used lambda expressions before , could you explain me a bit about the query you used.Thanks for the help.
Sure. Everything in Any is a lambda expression. The lambda expression here serves as a function like bool myFunction<T>(T x). We don't have to explicitly specify the type of x (T) thanks to the type inference feature of C#.
Also If I find records how do I get them , I mean how to use IEnumerable for getting results , a small example will be good enough for me.Thanks a lot.
the updated answer can be used if bool duplicateExists is true right.
@freebird see the update. nothing special about it, just use a foreach.
|
2

try this

var duplicate = (from dup in dt.Data_Customer_Logs
                 where dup.cPanGirNo == panno 
                       || dup.cEmail == email 
                       || dup.nMobileNo.ToString() == mobno
select dup).FirstOrDefault();

if(duplicate != null && duplicate.Any())
   //here logic of what should happend if there is something in db

7 Comments

Ok so you are suggesting me to check firstly if it is null or not and then use Any() , can I get all the records found.Thanks.
you should check first null always it's good practice to avoid NullReferenceException
The check on Any() is useless. If it doesn't find any match it will return null, if it finds a match it returns only the first occurrence...
@freebird If the first part of the && evaluates to false, the second part doesn't even get evaluated.
@freebird In this case, duplicate is a single item (or null) rather than an enumeration, so you should only do the null check. In general, the x != null && x.SomeProperty... pattern is a good one if you're worried x may be null. It's just that, here, the Any part makes no sense.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.