Get minimum value from list - asp.net

I have a class ABC like this
public class ABC{
public int Id {get;set;}
public int UserCount {get;set;}
}
Now I add following records to a list of type ABC
List<ABC> lstABC = new List<ABC>();
lstABC.Add(new ABC(){Id=1,UserCount=5});
lstABC.Add(new ABC(){Id=2,UserCount=15});
lstABC.Add(new ABC(){Id=3,UserCount=3});
lstABC.Add(new ABC(){Id=4,UserCount=20});
I've another list of type int
List<int> lstIds = new List<int>();
lstIds.Add(1);
lstIds.Add(3);
lstIds.Add(4);
Now i want to find out the minimum value of UserCount by comparing both the list where Id in lstABC should match the items presesnt in lstIds. I can get the minimum value of UserCount by using loops but is there any another way to get the value in an optimized way by avoiding loops?

You can use Enumerable.Join to link both lists:
var joined = from id in lstIds
join x in lstABC
on id equals x.Id
select x;
int minValue = joined.Min(x => x.UserCount);
This is more efficient than loops since Join uses a set to find matching items.
There's more than one way to skin a cat, this is a little bit less efficient:
int minValue = lstABC.Where(x => lstIds.Contains(x.ID)).Min(x => x.UserCount);

Try below code:
int min = lstABC.Min(entry => entry.UserCount);
var a = lstABC.Where(e => e.UserCount == min).FirstOrDefault();
var resul = lstIds.Where(e => e.Equals(a.Id)).FirstOrDefault();
var result = lstABC.Where(n =>n.Id == resul).FirstOrDefault();

Related

StoreRequestParameters,get the values issue

on the web service side I am applying
StoreRequestParameters parameters = new StoreRequestParameters(this.Context);
string condition= parameters.GridFilters.ToString();
//I ma sending this to the methot "List<Ks> Get(....)"
to get the gridfilter parameters.
inside the other methot ,trying to get the selected gridfilters values like this.
public List<Ks> Get(int start, int limit, string sort, string terssiralama, string condition, out int totalrow)
{
FilterConditions fc = new FilterConditions(condition);
foreach (FilterCondition cnd in fc.Conditions)
{
Comparison comparison = cnd.Comparison;
string fi = cnd.Field;
FilterType type = cnd.Type;
switch (cnd.Type)
{
case FilterType.Date:
switch (comparison)
{
case Comparison.Eq:
field1 = cnd.Field;
cmp1 = "=";
value1 = cnd.Value<string>();
...........
..........
}
but I failed getting the values like this
FilterConditions fc = new FilterConditions(condition);
I couldnt pass the string values .
should I serializes or deserilized first ?
StoreRequestParameters parameters = new StoreRequestParameters(this.Context);
instead of using this, string condition= parameters.GridFilters.ToString();
I use this
string obj = this.Context.Request["filter"];
and pass it to the
FilterConditions fc = new FilterConditions(obj);
It can be reach all filter condition in fc filtercondition variable.

Linq to Entity, selecting group with or without value

Hi Need some help with LINQ query.
I have entity called Shift. This entity has several value field but the ones I am intressted in are ShiftID (int), ShiftDate (DateTime) and GrossMount (decimal(10,2). And this needs to be grouped by month (binding this to a graph in ASP.NET).
I need data for the last 12 months grouped by month.
I have come a bit on the way with this post: Linq to Entity, selecting group without value but not quite all the way.
This is my code for now:
public IQueryable<Shift> GetPastMonths(int months, string accountNumber)
{
_context = new EtaxiEnteties();
var _date = DateTime.Today;
var _firstOfMonth = new DateTime(_date.Year, _date.Month, 31);
var _twelveMonthAgoFirstOfMonth = _firstOfMonth.AddMonths(-12);
// Generate a collection of the months and years for the last 12 months
var _monthYears = Enumerable.Range(-12, 12).Select(monthOffset => { var monthDate = _firstOfMonth.AddMonths(monthOffset); return new { y = monthDate.Year, m = monthDate.Month }; });
var _data = (from _monthYear in _monthYears
join _i in
(from _i in _context.Shifts.Where(acc => acc.Account.AccountNumber == accountNumber)
where _i.ShiftDate >= _twelveMonthAgoFirstOfMonth && _i.ShiftDate < _firstOfMonth
group _i by new { y = _i.ShiftDate.Year, m = _i.ShiftDate.Month } into g
select new { ShiftID = g.Key, GrossAmount = g.Count() }) on _monthYear equals _i.ShiftID into j
from _k in j.DefaultIfEmpty()
select new Shift() { ShiftDate = new DateTime(_monthYear.y, _monthYear.m, 1), GrossAmount = _k != null ? _k.GrossAmount : 0 });
return _data as IQueryable<Shift>;
}
Now I have in return a collection of Shift objects, grouped by month but still missing the GrossAmount. Althoug i would need this from today date (only getting from 1 of current month).
Believe this is my main problem: GrossAmount = g.Count(), but I am not sure
Any LINQ specialist out there that could give me a push?
Use GrossAmount = g.Sum(x => x.GrossAmount) to calculate total GrossAmount value of grouped Shift entities. I believe you have typo in GrossAmount (GrossMount) property name.

Asp.net, C#, Linq and Array usage,

var result = (from p in productInQuery
join o in orderInfoQuery on p.refNo equals o.refNo
join x in productQuery on p.productNo equals x.no
join t in productOutQuery on p.no equals t.productInNo into productIn
from t in productIn.DefaultIfEmpty()
//let dateOut = (from m in orderInfoQuery where m.refNo == t.refNo select m.delivered).FirstOrDefault()
orderby o.processDate descending
select new
{
modelNo = x.modelNo,
mfgNo = p.mfgNo,
serialNo = p.serialNo,
poNo = p.poNo,
lbs = p.lbs,
width = p.width,
height = p.height,
depth = p.depth,
qty = p.qty,
dateIn = o.processDate,
dateOut = (DateTime?)(from m in orderInfoQuery where m.refNo == t.refNo select m.processDate).FirstOrDefault()
}).ToArray();
I want to add this result into iTextSharp table cell.
but I don't know how to do,
I tried,
int resultSize = result.Length;
/*
for (int j = 0; j < resultSize; j++)
{
table.AddCell(result[j]);
}
*/
foreach (string[] test in result) //Error : Unable to cast object of type
{
int testSize = test.Length;
for (int j = 0; j < testSize; j++)
{
table.AddCell(test[j]);
}
}
but I lost way :(
anybody knows, please advice me~
[EDIT]
int resultSize = result.Length; //192
test does have a fields...
[EDIT 2]
Actually the result be returned from Model.
PJ.WebUI.Models Reports Class
public class Reports
{
public Array GetTransaction(){
OrdersRepository ordersRepository = new OrdersRepository();
var productInQuery = ordersRepository.ProductIn;
var productOutQuery = ordersRepository.ProductOut;
var productQuery = ordersRepository.Product;
var orderInfoQuery = ordersRepository.OrderInfo;
var result = (from p in productInQuery
join o in orderInfoQuery on p.refNo equals o.refNo
join x in productQuery on p.productNo equals x.no
join t in productOutQuery on p.no equals t.productInNo into productIn
from t in productIn.DefaultIfEmpty()
//let dateOut = (from m in orderInfoQuery where m.refNo == t.refNo select m.delivered).FirstOrDefault()
orderby o.processDate descending
select new
{
modelNo = x.modelNo,
mfgNo = p.mfgNo,
serialNo = p.serialNo,
poNo = p.poNo,
lbs = p.lbs,
width = p.width,
height = p.height,
depth = p.depth,
qty = p.qty,
dateIn = o.processDate,
dateOut = (DateTime?)(from m in orderInfoQuery where m.refNo == t.refNo select m.processDate).FirstOrDefault()
}).ToArray();
return result;
}
}
And in controller call the method to get the result.
Reports reposrts = new Reports();
var result = reposrts.GetTransaction();
..
foreach (var test in result)
{
table.AddCell(test.modelNo);
}
Then
Error 1 'object' does not contain a definition for 'modelNo' and no extension method 'modelNo' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
So,
I move the model method into controller method (put all together in same method) and run. then it works!.
but the result be used in the other controller too. so I want to keep the result in Model class to reuse.
I want to know why it does not work if the result query is not in same method.
could you help me a little more please?
Thank you very much!
'result' will be an array of the anonymous type you selected to, not an array of string arrays, which is why you are getting an error in the foreach loop.
Your loop should probably look more like the following:
foreach (var test in result)
{
table.AddCell(test.modelNo);
table.AddCell(test.mfgNo);
table.AddCell(test.serialNo);
// etc
}
#Edit 2:
Because the result of GetTransaction() is 'Array', the calling method has no idea what the type is, it just sees it as an array of objects, and, because it is an anonymous type, you can't (reasonably) cast it back to the expected type.
I think your best bet in this case would be to make a new class which has the properties you want to return, e.g.:
public class Transaction
{
public string ModelNo { get; set; }
public string MfgNo { get; set; }
public string SerialNo { get; set; }
// etc. - but with the correct types for the properties
}
Then change the linq query to select into this type, instead of the anonymous type, e.g.:
...
orderby o.processDate descending
select new Transaction
{
ModelNo = x.modelNo,
MfgNo = p.mfgNo,
SerialNo = p.serialNo,
// etc.
...
And change the return type of GetTransaction() from Array to Transaction[].
It's not clear what you are trying to do but I believe you are casting each element of your result as a string incorrectly.
Try this:
foreach (var test in result)
You will probably need to tweak your inner code since test isn't an array, but that should get you past your current error.

How to calculate count on of table column using group by clause in linq

I'm new to linq.
In c# I'm doing as follows to get the count of one column.
SELECT DispatcherName,
ActivityType,
CONVERT(BIGINT,COUNT(ActivityType)) AS Total
FROM ACTIVITYLOG
GROUP BY DispatcherName,
ActivityType
ORDER BY Total DESC
Can any one tell m,how I can achieve the same thing using LINQ.
Update:
HI I did as follows and got the reslut.
But I'm not able to convert result to datatable.
this is how I did.
here dt is datatabe with two columns Dispatchername and ActivityType.
var query1 = from p in dt.AsEnumerable()
group p by new
{
DispatcherName = p.Field<string>("Dispatchername"),
Activity = p.Field<string>("ActivityType"),
}
into pgroup
let count = pgroup.Count()
orderby count
select new
{
Count = count,
DispatcherName = pgroup.Key.DispatcherName,
Activity = pgroup.Key.Activity
};
pls help me out asap.
from c in ACTIVITYLOG
group c by new {c.DispatcherName, c.ActivityType} into g
orderby g.Count() descending
select new { g.Key.DispatcherName, g.Key.ActivityType, Total = g.Count() }
If you want your results returned back to a DataTable, one option is to use the CopyToDataTable method.
Here's a live example: http://rextester.com/XHX48973
This method basically requires you to create a dummy table in order to use its NewRow method - the only way to create a DataRow, which is required by CopyToDataTable.
var result = dt.AsEnumerable()
.GroupBy(p => new {
DispatcherName = p.Field<string>("DispatcherName"),
Activity = p.Field<string>("ActivityType")})
.Select(p => {
var row = dummy.NewRow();
row["Activity"] = p.Key.Activity;
row["DispatcherName"] = p.Key.DispatcherName;
row["Count"] = p.Count();
return row;
})
.CopyToDataTable();
Perhaps a better way might be just fill in the rows directly, by converting to a List<T> and then using ForEach.
DataTable dummy = new DataTable();
dummy.Columns.Add("DispatcherName",typeof(string));
dummy.Columns.Add("Activity",typeof(string));
dummy.Columns.Add("Count",typeof(int));
dt.AsEnumerable()
.GroupBy(p => new { DispatcherName = p.Field<string>("DispatcherName"),
Activity = p.Field<string>("ActivityType")})
.ToList()
.ForEach(p => {
var row = dummy.NewRow();
row["Activity"] = p.Key.Activity;
row["DispatcherName"] = p.Key.DispatcherName;
row["Count"] = p.Count();
dummy.Rows.Add(row);
});
Live example: http://rextester.com/TFZNEO48009
This should do the trick:
IList<ACTIVITYLOG> allActivityLogs;
var result = (from c in allActivityLogs
select new
{
DispatcherName = c.DispatcherName,
ActivityType = c.ActivityType,
Total = c.ActivityType.Count
}).OrderByDescending(x => x.Total)
.GroupBy(x => new { x.DispatcherName, x.ActivityType });
You only need to substitute the allActivityLogs collection with the actual collection of your entities.

How to do Previous and Next item on a List<G>

I have a class called Person, which contains various properties such as first name, last name, etc.
I have a List that contains n instances of Person.
List<Person> lstPerson = new List<Person>();
What I need to be able to do is search lstPerson for a given person, and having found that person then obtain the previous and next person relative to the person's item position in lstPerson.
Person p = lstPerson.Find(delegate(Person o) { return o.FirstName == "Joe"; });
// Then use an accessor to get the previous and next persons
Person prevPerson = p.Previous();
Person nextPerson = p.Next();
Is there a better way to do this then the one that I stated above? What I don't like about the above solution is that I have to construct the previous and next pointers at the time that I build the Person List.
You could do something like the following:
int personIndex = lstPerson.FindIndex(delegate(Person o) { return o.FirstName == "Joe" });
Person p = lstPerson[personIndex];
// verify that personIndex+1 and personIndex-1 still fall within the list
Person prevPerson = lstPerson[personIndex-1];
Person nextPerson = lstPerson[personIndex+1];
Hmm.. why not simply using the LinkedList class?
It comes with Next and Previous built in.
You could do this, which skips elements until the next element matches or it has gone through the entire list:
var list = (new List<Person>() { null }).Concat(lstPerson);
var p = list.SkipWhile((x, index) => { var n = list.ElementAtOrDefault(index + 1); return n == null || n.FirstName != name; }).Take(3);
Person previous = p.FirstOrDefault();
Person person = p.ElementAtOrDefault(1);
Person next = p.ElementAtOrDefault(2);
Although this (similar to #marcind's answer) might be clearer:
Person person = lstPerson.Find(p => p.FirstName == name);
Person previous = null;
Person next = null;
if (person != null)
{
int index = lstPerson.IndexOf(person);
previous = lstPerson.ElementAtOrDefault(index - 1);
next = lstPerson.ElementAtOrDefault(index + 1);
}

Resources