c# - Check existence of a record before returning resultset in LINQ to SQL -


I'm looking for a simple solution to change my standardized junk method by trying to get data Whether or not a record exists before. At present, whenever one of my methods is called, I do something for its effect ...

  Private Recognition Dosoming (Int ID) {if (Data records. Count (q = & gt; Q.Id == ID) & gt; 0) {Return Data Records First (q = & gt; q.id == ID); } Return tap; }  

... where I always check the count of records to determine the existence of a record. There should be another "elegant" way to do this without calling the database twice, is there any way?

There are several clean ways to handle this. If you want to first record related to id then you can say:

  record record = data. Record. FirstOver Default (R = & gt; RID == ID); If you want to know only if there is any such  record     : 

  return data . Records. Annie (R => RIID == ID); // If true, then  

If you want to see how many such records exist:

  return data. Calculation (r = & gt; rid == ID);  

If you list all such records :

  IEnumerable & lt; record & gt; ) Want Return Data Record Where (R => RIID == ID);  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -