I have created a MySQL Database with a vast number of products and their cost. I utilize EF6 to wrap the database.
Based on the given input, I need to generate at random, a correct selection that meets the described criteria.
For example:
10 Items, Total Value $25
I am at a loss as how to properly go about iterating through the database to produce the required results.
What I am currently doing seems terribly inefficent:
using (var db = new Database())
{
var packageSelected = false;
var random = new Random();
var minItemId = (from d in db.products select d.id).Min();
var maxItemId = (from d in db.products select d.id).Max();
var timer = new Stopwatch();
timer.Start();
Console.WriteLine("Trying to make package...");
while (!packageSelected)
{
var currentItems = new List<int>();
for (var i = 0; i <= 9; i++)
{
var randomItem = random.Next(minItemId, maxItemId);
currentItems.Add(randomItem);
}
decimal? packageValue = 0;
currentItems.ForEach(o =>
{
var firstOrDefault = db.products.FirstOrDefault(s => s.id == o);
if (firstOrDefault != null)
{
var value = firstOrDefault.MSRP;
packageValue += value;
}
});
if (!(packageValue >= 25) || !(packageValue <= 26)) continue;
packageSelected = true;
timer.Stop();
Console.WriteLine("Took {0} seconds.", timer.Elapsed.TotalSeconds);
currentItems.ForEach(o =>
{
var firstOrDefault = db.products.FirstOrDefault(s => s.id == o);
if (firstOrDefault != null)
Console.WriteLine("Item: {0} - Price: ${1}", firstOrDefault.DESCRIPTION,
firstOrDefault.MSRP);
});
}
}
What about something like this:
public virtual TEntity GetRandom()
{
return DBSet.OrderBy(r => Guid.NewGuid()).Take(1).First();
}
public List<TEntity> Random(int amount, int maxprice)
{
var list = new List<TEntity>();
var tempPrice = 0;
for (int i = 0 ; i < amount; i++)
{
var element = GetRandom();
tempPrice += element.Price;
if (tempPrice > maxprice)
{
return list;
}
list.Add(element);
}
return list;
}
hope this helps
EDIT: If the maxprice is reached before the required amount of elements, the for-loop will stop and you won't get the full amount of elements.
Related
I have a linq which is inside a for loop,im adding the results to a list using addRange() but it will add whole thing in a single set,for example my first loop result has 16 items,the second has 10 items,...i want them to be added to list like this then i can see in list how many and which items has been added on each query
public List<statisticsDaily> dailyStat(List<string> id,string dtFrom,string dtTo)
{
List<StatisticsDaily> rsltofquery = new List<StatisticsDaily>();
for (int i = 0; i < id.Count; i++)
{
var rslt = (from d in db.statDaily
join s in db.masterData on d.m_turbine_id equals s.m_turbine_id
where d.m_turbine_id == IPAddress.Parse(id[i]) && d.m_date >= frm && d.m_date <= to
select new StatisticsDaily
{
m_wind_speed = d.m_wind_speed,
Date = d.m_date.ToString("yyyy-MM-dd"),
name = s.turbine_name,
Production = d.m_energy_prod,
Availability = d.m_corrected_av
}
).AsEnumerable().OrderBy(s => s.Date).ToList();
rsltofquery.AddRange(rslt);
}
You need to have collection of collections like List<List<StatisticsDaily>>.
So yours code will be:
public List<List<statisticsDaily>> dailyStat(List<string> id,string dtFrom,string dtTo)
{
List<List<StatisticsDaily>> rsltofquery = new List<List<StatisticsDaily>>();
for (int i = 0; i < id.Count; i++)
{
var rslt = (from d in db.statDaily
join s in db.masterData on d.m_turbine_id equals s.m_turbine_id
where d.m_turbine_id == IPAddress.Parse(id[i]) && d.m_date >= frm && d.m_date <= to
select new StatisticsDaily
{
m_wind_speed = d.m_wind_speed,
Date = d.m_date.ToString("yyyy-MM-dd"),
name = s.turbine_name,
Production = d.m_energy_prod,
Availability = d.m_corrected_av
}).AsEnumerable().OrderBy(s => s.Date).ToList();
rsltofquery.Add(rslt);
}
}
If you want to use all elements, not in parts, you can use SelectMany:
var x = dailyStat(id, dtFrom, dtTo);
foreach (var e in x.SelectMany(d => d)) ...
I would like to ask you if there's a way by Linq to check discontinuity of multiple ranges, for example we have a class AgeRange:
public class AgeRange
{
public int firstValue {get;set;}
public int secondValue {get;set;}
}
var ageRange1 = new AgeRange(0,2); // interval [0,2]
var ageRange2 = new AgeRange(4,10); // interval [4,10]
var ageRange3 = new AgeRange(11,int.MaxValue); // interval [11,+oo[
var ageRangeList = new List<AgeRange>();
ageRangeList.Add(ageRange1);
ageRangeList.Add(ageRange2);
ageRangeList.Add(ageRange3);
in this example we have a discontinuity between first range and second range.
is there a way in Linq to check discontinuity between elements in ageRangeList ?
Thanks for you help.
Assuming firstValue always <= secondValue (for the same element), you can try to use Aggregate:
var start = ageRangeList
.OrderBy(a => a.firstValue).Dump()
.First();
var result = ageRangeList
.OrderBy(a => a.firstValue)
.Aggregate(
(hasGap: false, s: start.secondValue),
(tuple, range) =>
{
if (tuple.hasGap)
{
return tuple;
}
else
{
var max = Math.Max(tuple.s, tuple.s+1); //hacky overflow protection
if (max < range.firstValue)
{
return (true, tuple.s);
}
else
{
return (false, Math.Max(tuple.s, range.secondValue));
}
}
})
.hasGap;
The downside of such approach is that it still will need to loop through all age ranges.
If you want to find first discontinuity and use that information elsewhere
public static IEnumerable<AgeRange> FindDiscontinuity(List<AgeRange> ageRangeList) {
foreach(var ageRange in ageRangeList.Zip(ageRangeList.Skip(1), (a, b) => new {Prev = a, Current = b})) {
if(ageRange.Prev.SecondValue != ageRange.Current.FirstValue) {
yield return ageRange.Prev;
yield return ageRange.Current;
break;
}
}
}
public static void Main()
{
var ageRange1 = new AgeRange(0, 2);
var ageRange2 = new AgeRange(4, 10);
var ageRange3 = new AgeRange(11, int.MaxValue);
var ageRangeList = new List<AgeRange>();
ageRangeList.Add(ageRange1);
ageRangeList.Add(ageRange2);
ageRangeList.Add(ageRange3);
var result = FindDiscontinuity(ageRangeList);
foreach(var ageRange in result) {
Console.WriteLine("{0}, {1}", ageRange.FirstValue, ageRange.SecondValue);
}
}
You can change the function so it can return boolean value instead of data.
I have a report screen where I do group by over 200 MB of data in DB and sometimes i get timeout, sometimes runs to completion.
How can i optimize it or make it faster?
Below is the code I have :
public ActionResult CleanReport()
{
var count = _db.Surveys.AsNoTracking().Count();
var result = _db.Surveys.AsNoTracking().GroupBy(x => x.Clean).Select(group => new { Clean = group.Key, Count = group.Count() });
var items = new List<PieChartModel>();
foreach (var item in result)
{
if (item.Clean.HasValue == false)
{
continue;
}
var chartModel = new PieChartModel();
if (item.Clean == Clean.IdareEder)
{
chartModel.color = "#F7464A";
chartModel.highlight = "#FF5A5E";
}
if (item.Clean == Clean.Iyiydi)
{
chartModel.color = "#46BFBD";
chartModel.highlight = "#5AD3D1";
}
if (item.Clean == Clean.Kötüydü)
{
chartModel.color = "#642EFE";
chartModel.highlight = "#642EFE";
}
if (item.Clean == Clean.CevapYok)
{
chartModel.color = "#FFCC00";
chartModel.highlight = "#FFCC66";
}
chartModel.label = item.Clean.Value.DisplayName();
double per = (item.Count * 100.0) / count;
chartModel.value = (int)Math.Round(per);
items.Add(chartModel);
}
return Json(items, JsonRequestBehavior.AllowGet);
}
Suppose I have this number list:
List<int> nu = new List<int>();
nu.Add(2);
nu.Add(1);
nu.Add(3);
nu.Add(5);
nu.Add(2);
nu.Add(1);
nu.Add(1);
nu.Add(3);
Keeping the list items in the same order, is it possible to group the items in linq that are sum of 6 so results would be something like this:
2,1,3 - 5 - 2,1,1 - 3
Solving this with LINQ directly would be bothersome, instead you could make an extension method:
// Assumptions:
// (1) All non-negative, or at least you don't mind them in your sum
// (2) Items greater than the sum are returned by their lonesome
static IEnumerable<IEnumerable<int>> GroupBySum(this IEnumerable<int> source,
int sum)
{
var running = 0;
var items = new List<int>();
foreach (var x in source)
{
if (running + x > sum && items.Any())
{
yield return items;
items = new List<int>();
running = 0;
}
running += x;
items.Add(x);
}
if (items.Any()) yield return items;
}
You could do it with Aggregate.
(Side note: Use LinqPad to test/write these types of queries, makes it easy)
Gives these results:
Like this:
class Less7Holder
{
public List<int> g = new List<int>();
public int mySum = 0;
}
void Main()
{
List<int> nu = new List<int>();
nu.Add(2);
nu.Add(1);
nu.Add(3);
nu.Add(5);
nu.Add(2);
nu.Add(1);
nu.Add(1);
nu.Add(3);
var result = nu .Aggregate(
new LinkedList<Less7Holder>(),
(holder,inItem) =>
{
if ((holder.Last == null) || (holder.Last.Value.mySum + inItem >= 7))
{
Less7Holder t = new Less7Holder();
t.g.Add(inItem);
t.mySum = inItem;
holder.AddLast(t);
}
else
{
holder.Last.Value.g.Add(inItem);
holder.Last.Value.mySum += inItem;
}
return holder;
},
(holder) => { return holder.Select((h) => h.g );} );
result.Dump();
}
private static Game[] getMostPlayedGamesDo(int Fetch, int CategoryID)
{
Game[] r;
using (MainContext db = new MainContext())
{
if (CategoryID == 0)
{
var q = db.tblArcadeGames.OrderByDescending(c => c.Plays).Take(Fetch);
r = new Game[q.Count()];
int i = 0;
foreach (var g in q)
{
r[i] = new Game(g);
i++;
}
}
else
{
var q = db.tblArcadeGames.Where(c=>c.CategoryID == CategoryID).OrderByDescending(c => c.Plays).Take(Fetch);
r = new Game[q.Count()];
int i = 0;
foreach (var g in q)
{
r[i] = new Game(g);
i++;
}
}
}
return r;
}
I can't seem to define q outside the scope of the if, and I can't insert the returned values to the array outside the scope of the if! Not sure how to remove repeating code in this simple instance?
It's not clear what the type of q is -- but deducing from your usage:
db.tblArcadeGames.OrderByDescending(...)
Presumably it's an entity class from Linq-To-Sql or Entity Framework. In that case, you do have a concrete entity defined, presumably named tblArcadeGame. Therefore, move q out of the scope by not using var:
IQueryable<tblArcadeGame> q;
if (CategoryID == 0)
{
q = db.tblArcadeGames.OrderByDescending(c => c.Plays).Take(Fetch);
}
else
{
q = db.tblArcadeGames.Where(c=>c.CategoryID == CategoryID).OrderByDescending(c => c.Plays).Take(Fetch);
}
r = new Game[q.Count()];
int i = 0;
foreach (var g in q)
{
r[i] = new Game(g);
i++;
}
As you can see, the repeated code is now seen only once.
P.S. Tools like ReSharper are fantastic for this sort of thing. Using it, with one keystroke you can toggle between the var version and that using explicitly named types.
You should really just explicitly type q. But this may let you get away without it (ternary operator will enforce it for you).
var q = CategoryID == 0 ? db.tblArcadeGames.OrderByDescending(c => c.Plays).Take(Fetch)
: db.tblArcadeGames.Where(c=>c.CategoryID == CategoryID).OrderByDescending(c => c.Plays).Take(Fetch);
r = new Game[q.Count()];
int i = 0;
foreach (var g in q)
{
r[i] = new Game(g);
i++;
}
I assume q is type IQueryable.
private static Game[] getMostPlayedGamesDo(int Fetch, int CategoryID)
{
var q = db.tblArcadeGames;
if (CategoryID != 0)
{
q = q.Where(c => c.CategoryID == CategoryID);
}
q = q.OrderByDescending(c => c.Plays).Take(Fetch);
return q.Select(g => new Game(g)).ToArray();
}
List<tblArcadeGame> q;
/* object q; */
if (CategoryID == 0)
{
q = db.tblArcadeGames.OrderByDescending(c => c.Plays).Take(Fetch).ToList();
}
else
{
q = db.tblArcadeGames.Where(c=>c.CategoryID == CategoryID).OrderByDescending(c => c.Plays).Take(Fetch).ToList();
}
r = new Game[q.Count()];
int i = 0;
foreach (var g in q)
{
r[i] = new Game(g);
i++;
}
I'll assume q is List<tblArcadeGame>