How to remove repeating code in this method? - c#

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>

Related

how to add linq result to list with key

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)) ...

Update list by another list (linq)

I have List of object of class "Data" that look like:
class Data
{
int code;
string name;
...
DateTime date_update;
}
and I have another list of class, like:
class RefCodes
{
int old_code;
int new_code;
string new_name;
DateTime date_update;
}
The list of "Data" contains like 1,000 objects.
The list of "RefCodes" contains like 30 objects.
I need to replace in list "Data",
the fields:
"code" to be with value of "new_code",
and the "name" to be with value of "new_name".
The replacement need to be only for the objects that their code exist in list "RefCodes".
by the query: if code in Data.code == RefCodes.old_code
How can I do it?
I think you're looking for this:
foreach (var rcodeObj in RefCode)
{
foreach(var obj in (Data.Where(t => t.code == rcodeObj.old_code)))
{
obj.code = rcodeObj.new_code;
obj.name = rcodeObj.new_name;
}
}
If you are using C#6 you could use linq to do something like this
var updatedData = data.Select(x => new Data
{
code = refCodes.FirstOrDefault(y => y.old_code == x.code)?.new_code ?? x.code,
name = refCodes.FirstOrDefault(y => y.old_code == x.code)?.new_name ?? x.name,
});
You can use the following code:
foreach (var x in DataList)
{
var itemRefCode = RefCodesList.FirstOrDefault(d => d.old_code == x.code);
if (itemRefCode != null)
{
x.code = itemRefCode.new_code;
x.name = itemRefCode.new_name;
}
}
You can iterate through each of the lists and update the values as follows. Here I am using some sample inputs as shown below. Note that I am considering the fields of the classes to be public, for simplicity:
List<Data> dataList = new List<Data>
{
new Data { code = 1, name = "A" },
new Data { code = 2, name = "B" },
new Data { code = 10, name = "C" },
};
List<RefCodes> refList = new List<RefCodes>
{
new RefCodes { old_code = 1, new_code = 11, new_name = "X" },
new RefCodes { old_code = 2, new_code = 22, new_name = "Y" }
};
Console.WriteLine("Before");
dataList.ForEach(data => Console.WriteLine(data.code + ": " + data.name));
Console.WriteLine("");
Here is the code to do the updating:
foreach (var refCodes in refList)
{
foreach (var data in dataList)
{
if (data.code == refCodes.old_code)
{
data.code = refCodes.new_code;
data.name = refCodes.new_name;
}
}
}
Console.WriteLine("After");
dataList.ForEach(data => Console.WriteLine(data.code + ": " + data.name));
Output:
Before
1: A
2: B
10: C
After
11: X
22: Y
10: C
Would this solve your problem:
public void Update( List<Data> data, List<RefCodes> refCodes )
{
List<RefCodes> differences = refCodes
.Where( r => data.Any( d => r.old_code == d.code ) )
.ToList();
differences.ForEach( ( RefCodes item ) =>
{
Data element = data.FirstOrDefault( d => d.code == item.old_code );
element.code = item.new_code;
element.name = item.new_name;
} );
}
What you need is a Left Outer Join.
For example,
IEnumerable<Data> query = from data in dataList
join refCode in refList on data.code equals refCode.old_code into joined
from subCode in joined.DefaultIfEmpty()
select new Data
{
code = subCode?.new_code ?? data.code,
name = subCode?.new_name ?? data.name,
date_update = subCode == null ? data.date_update : DateTime.Now
};
will return a sequence with the result you expect.
**Let say tempAllocationR is list 1 and tempAllocationV is List2 **
var tempAllocation = new List<Object>();
if (tempAllocationR.Count > 0 && tempAllocationV.Count > 0)
{
foreach (TempAllocation tv in tempAllocationV)
{
var rec = tempAllocationR.FirstOrDefault(tr => tr.TERR_ID == tv.TERR_ID && tr.TERR == tv.TERR && tr.Team == tv.Team);
if (rec != null)
{
rec.Vyzulta = tv.Vyzulta;
}
else
{
tempAllocationR.Add(tv);
}
}
tempAllocation = tempAllocationR;
}
else if (tempAllocationV.Count == 0 && tempAllocationR.Count > 0)
{
tempAllocation = tempAllocationR;
}
else if (tempAllocationR.Count == 0 && tempAllocationV.Count > 0)
{
tempAllocation = tempAllocationV;
}

Randomly Selected Data from Database with Constraints

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.

How to update object record in an ArrayList

I want to update record in ArrayList in c#, I want to update my status to inactive of particular record of ArrayList, here is the code below but this isn't working fine as it doesn't update status to inactive.
public Boolean changeAccStatus(int userid)
{
Boolean result = false;
for (int i = 0; i < list.Count; i++)
{
AccountDto dto = (AccountDto)list[i];
if (dto.Userid == userid) {
dto.Status = "Unactive";
list[i] = dto;
result = true;
}
}
return result;
}
Do (AccountDto)list[i].Status = "inactive";
try with below code
foreach (var temp in list)
{
var t = temp as AccountDto;
if (t != null && t.Userid.Trim().Equals(userid.Trim()))
t.Status = "inactive";
}
also think about doing re factoring to generic List< AccountDto> , this will give you the power of Linq directly
if you can able to convert list to generic list than you can do this
Func<int, AccountDto, bool> UpdateRecord= (y, x) =>
{
if( x.Userid== y){x.Status="Unactive";
return true;
};
List<AccountDto> mylist = new List<AccountDto>();
var v = from x in mylist
where UpdateRecord(userit-intvalue, x)
select x;
also you can try this
List<AccountDto> mylist = new List<AccountDto>();
list.ForEach(x => {
if( x.Userid== userid)
x.Status="Unactive";
});

Entity many to many check if relation exists

I have two tables: Items and Colors. They have a many to many relation. In a CheckBoxList that displays colors, I want to check those that are associated to the item shown.
using (var db = new ProwebModel.Entities())
{
var colors = db.Colors;
foreach (ListItem color in ((CheckBoxList)(fv.FindControl("cblColors"))).Items)
{
var itemId = Convert.ToInt32(Request.QueryString["id"]);
var colorNumber = Convert.ToInt32(color.Value);
color.Selected = colors.Where(t => t.ColorNumber == colorNumber).First().Items.Where(t => t.ItemId == itemId).Count() > 0;
}
}
This works fine, but I was wondering about this line :
color.Selected = colors.Where(t => t.ColorNumber == colorNumber).First().Items.Where(t => t.ItemId == itemId).Count() > 0;
Is there any better way to check if the association exist?
Thank you!
EDIT
I chaged my code to something better.. I think. Is there still a better way to do this?
using (var db = new ProwebModel.Entities())
{
var itemId = Convert.ToInt32(Request.QueryString["id"]);
var ItemColors = db.Items.First(t => t.ItemId == itemId).Colors.ToList();
foreach (ListItem color in ((CheckBoxList)(fv.FindControl("cblColors"))).Items)
{
var colorNumber = Convert.ToInt32(color.Value);
color.Selected = ItemColors.Where(t => t.ColorNumber == colorNumber).Count() > 0;
}
}
Many thanks!
Code update
using (var db = new ProwebModel.Entities())
{
var itemId = Convert.ToInt32(Request.QueryString["id"]);
var ItemColors = db.Items.First(t => t.ItemId == itemId).Colors.ToList();
foreach (ListItem color in ((CheckBoxList)(fv.FindControl("cblColors"))).Items)
{
var colorNumber = Convert.ToInt32(color.Value);
color.Selected = ItemColors.Any(t => t.ColorNumber == colorNumber);
}
}
The line could at least be rewritten as :
color.Selected = colors.First(t => t.ColorNumber == colorNumber).Items.Any(t => t.ItemId == itemId);

Categories

Resources