How to apply a LINQ (l2s) query to a list of entites?
I imported a number of CSVs in a database, each is now a table with some common columns. I'm trying to figure out if I can use Linq2Sql to combine them together without doing a copy/modify.
I've never done anything like Dynamic LINQ Queries Is that the only thing I can do or are there other options?
The query is:
var excelRows =
from c in Apr10s // <-- GOAL - run on Apr10s, May10s, Jun10s, ... etc....
where
c.VolumeStats != null
&& c.VolumeStats > 0
&& c.Costperunit != null
&& c.Costperunit > 0
select new
{
c.Publisher,
c.CampaignNumber,
c.Campaign,
c.RevenuePerUnit,
c.Costperunit,
c.VolumeStats,
c.Rep
};
var reportItems =
from c in excelRows
select new
{
Publisher = c.Publisher,
Campaign = c.Campaign,
Payout = c.Costperunit,
Actions = c.VolumeStats,
Revenue = (decimal)c.Costperunit.Value * (decimal)c.VolumeStats.Value
};
var reportItemsByPubl开发者_如何学Pythonisher = reportItems.GroupBy(c => c.Publisher);
Not too easily. If you were using Linq to Entites it's very simple to select from a table using a runtime variable table name. Linq to Sql is more of a challenge.
You might instead considering writing a stored proc or a view that consolidates all the data in your database. This is a data issue, not an application issue, right?
edit: if you want to use the Entity Framework, you have a variety of options. The first is executing SQL & returning objects.
Alternatively, create an object and return it from your context for each table something like this
using (var c = new myContext())
{
var result1 = c.CreateObjectSet<MyType>("table1");
result1.Where(x => blah).Select(y => etc)
var result2 = c.CreateObjectSet<MyType>("table2");
var result3 = c.CreateObjectSet<MyType>("table3");
}
精彩评论