Factor out common LINQ elements (linqconect)
Posted: Fri 04 May 2012 14:19
If you look at the code below, you see a great deal of repetition in reading and extracting the values from a LinqQuery Table. It is easy enough to create a function that would accept a dotconnect context model (in this case the context is GPL, and the context Model is GPL.GPLModel), but what about the c.Type, c.Creator, c.Distributor, c.Handling). I have looked all through the methods of GPL.GPLModel (created by LinqConnect, but I am still not getting it.
As you can see, I was able to factor out some code into the private makeResponse method.
As you can see, I was able to factor out some code into the private makeResponse method.
Code: Select all
private ItemList makeResponse(IEnumerable<Item> data)
{
Item[] results = data.ToArray<Item>();
ItemList response = new ItemList
{
count = results.Length,
results = results
};
return response;
}
public ItemList Mcreators()
{
GPL.GPLModel context = new GPL.GPLModel();
var rows =
from c in context.MCreators
select new Item
{
idx = c.Idx,
name = c.Creator
};
return makeResponse(rows);
}
public ItemList Mtypes()
{
GPL.GPLModel context = new GPL.GPLModel();
var rows =
from c in context.MTypes
select new Item
{
idx = c.Idx,
name = c.Type
};
return makeResponse(rows);
}
public ItemList Mhandlings()
{
GPL.GPLModel context = new GPL.GPLModel();
var rows =
from c in context.MHandlings
select new Item
{
idx = c.Idx,
name = c.Handling
};
return makeResponse(rows);
}
public ItemList Mdistributors()
{
GPL.GPLModel context = new GPL.GPLModel();
var rows =
from c in context.MDistributors
select new Item
{
idx = c.Idx,
name = c.Distributor
};
return makeResponse(rows);
}
public ItemList McolumnNames()
{
PropertyInfo[] propertyInfos;
propertyInfos = typeof(CatalogEntry).GetProperties(BindingFlags.Public | BindingFlags.Instance);
int id = 0;
var rows =
from p in propertyInfos
select new Item
{
idx = id++,
name = p.Name
};
return makeResponse(rows);
}