I have a Linq query that I want to call from multiple places:
var myData = from a in db.MyTable
where a.MyValue == "A"
select new {
a.Key,
a.MyValue
};
How can I create a method, put this code in it, and then call it?
public ??? GetSomeData()
{
// my Linq query
}
IQueryable and IEnumerable both work. But you want to use a type specific version, IQueryable<
T>
or IEnumerable <
T>
.
So you'll want to create a type to keep the data.
var myData = from a in db.MyTable
where a.MyValue == "A"
select new MyType
{
Key = a.Key,
Value = a.MyValue
};
IQueryable
So your method declaration would look like
public IQueryable GetSomeData()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With