Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to refactor C# EF code?
    text
    copied!<p>I've created a console application that is using EF. It works very well, but I know that it can be refactored. Here's my code:</p> <pre><code>CABEntities caContext = new CAEntities(); USEntities usContext = new USEntities(); var query = caContext.CA_companies .Where(c =&gt; c.coverage_status.Equals("active")) .Select(c =&gt; new DatapullFields.Datapull { Ticker = c.ticker.Trim(), Exchange = c.primary_exchange.Trim(), CompanyName = c.company_name.Trim() }); CreateCSVFile.WriteToCSVFile tf = new CreateCSVFile.WriteToCSVFile(query, "CA"); query = usContext.US_companies .Where(c =&gt; c.coverage_status.Equals("active_eq")) .Select(c =&gt; new DatapullFields.Datapull { Ticker = c.ticker.Trim(), Exchange = c.primary_exchange.Trim(), CompanyName = c.company_name.Trim() }); tf = CreateCSVFile.WriteToCSVFile(query, "US"); </code></pre> <p>Here's what I have now after the edits:</p> <pre><code>public class Datapull { public string CompanyName { get; set; } public string Exchange { get; set; } public string Ticker { get; set; } } public interface ICountryEntity { string CompanyName { get; set; } string coverage_status { get; set; } string Exchange { get; set; } string Ticker { get; set; } } public partial class CAEntity : Console.Interface.ICountryEntity { } public partial class USEntity : Console.Interface.ICountryEntity { } public static class ToDataPull { public static IEnumerable&lt;T&gt; ToDataPull&lt;T&gt;(this IEnumerable&lt;T&gt; entities) where T : ICountryEntity { return entities.Where(c =&gt; c.coverage_status.Equals("active")) .Select(c =&gt; new Console.DatapullFields.Datapull { Ticker = c.ticker.Trim(), Exchange = c.primary_exchange.Trim(), CompanyName = c.company_name.Trim() }); } } </code></pre> <p>How can I refactor this code?</p>
 

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