<p>I have a Linq query that I want to call from multiple places:</p>

<pre><code>var myData = from a in db.MyTable
             where a.MyValue == "A"
             select new  {
                            a.Key,
                            a.MyValue
                          };
</code></pre>

<p>How can I create a method, put this code in it, and then call it?</p>

<pre><code>public  ???  GetSomeData()
{
   // my Linq query
}
</code></pre>

[{'answer_id': 55110, 'author': 'Ryan Lanciaux', 'author_id': 1385358, 'author_profile': 'https://Stackoverflow.com/users/1385358', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx" rel="nofollow noreferrer"><code>IQueryable</code></a></p>\n\n<p>So your method declaration would look like</p>\n\n<pre><code>public IQueryable GetSomeData()\n</code></pre>\n'}, {'answer_id': 55120, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 2, 'selected': False, 'text': '<p>If you want to return, you need a type.</p>\n\n<p>Instead of <code>var</code>, declare using <code>IEnumerable&lt;&gt;</code> and return that variable. Iterating through it actually executes the query.</p>\n'}, {'answer_id': 55254, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 4, 'selected': True, 'text': '<p>IQueryable and IEnumerable both work. But you want to use a type specific version, IQueryable<code>&lt;</code>T<code>&gt;</code> or IEnumerable <code>&lt;</code>T<code>&gt;</code>.</p>\n\n<p>So you\'ll want to create a type to keep the data.</p>\n\n<pre><code>var myData = from a in db.MyTable\n             where a.MyValue == "A"\n             select new MyType\n             {\n                 Key = a.Key,\n                 Value = a.MyValue\n             };\n</code></pre>\n'}, {'answer_id': 55278, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': '<p>A generic method should give you intellisense:</p>\n\n<pre><code>public class MyType {Key{get;set;} Value{get;set}}\n\npublic IQueryable&lt;T&gt; GetSomeData&lt;T&gt;() where T : MyType, new() \n { return from a in db.MyTable\n          where a.MyValue == "A" \n          select new T {Key=a.Key,Value=a.MyValue};\n }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/55101', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3047/']