Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Justin, it is not odd..it is indeed what inheritance is intended for. Your CandidateBase class provides a base for your Candidate classes and because it is abstract it means that it provides some logic that you don't have to worry about later on. I think it is better explained with an example.</p> <p>Lets say you have 2 different Candidate classes and they both want to provide some functionality, lets say GetResume().. you may create an abstract class or an interface for this..in your case you made an abstract class as such</p> <pre><code>public class CandidateBase { //some logic that you might need to share between Candidates //such as storing Name, Age..etc // your abstract method public abstract String GetResume(); } </code></pre> <p>now lets say CandidateA gets his Resume from a specific Web Service </p> <pre><code>public class CandidateA : CandidateBase { public String GetResume() { //some logic to get Resume from some web service return resumeStr; } } </code></pre> <p>now lets say you have CandidateB and you store his resume somewhere on disk.</p> <pre><code>public class CandidateB : CandidateBase { public String GetResume() { //some logic to get Resume from disk return resumeStr; } } </code></pre> <p>at some point your code when you call GetCandidates() you won't have to worry about which type your candidate is, you can still get their resume by calling GetResume() on CandidateBase. </p> <p>btw Justin, if it really bothers you, you can always cast back to CandidateA after you call GetCandidates()</p> <pre><code>IQueryable&lt;CandidateA&gt; candidates = recruiterClass.GetCandidates().Cast&lt;CandidateA&gt;(); </code></pre> <p><strong>EDIT TO ADD</strong></p> <p>Justin I think this should fix your issue, let me know.</p> <pre><code>public abstract class CandidateBase { public int CandidateId { get; set; } public string LastName { get; set;} public string FirstName { get; set;} public string RecruiterId { get; set; } //the rest of your logic } public class RecruiterBase { // Constructors declared here // ----HERE IS WHERE I AM BREAKING DOWN---- public IQueryable&lt;T&gt; GetCandidates&lt;T&gt;() where T:CandidateBase, new() { DataClasses1DataContext db = new DataClasses1DataContext(); return (from candidates in db.Candidates where candidates.RecruiterId == this.RecruiterId select new T() { CandidateId = candidates.CandidateId, LastName = candidates.LastName, FirstName = candidates.FirstName, RecruiterId = candidates.RecruiterId } ) } } </code></pre> <p>you can use this like this</p> <pre><code>IQueryable&lt;CandidateA&gt; candidates = recruiterClass.GetCandidates&lt;CandidateA&gt;(); </code></pre> <p>I think this is a much cleaner solution, than all those Casts :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload