dynamic list in C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
if(some condition)
{
var columnSeries = (from l in logs
group l by l.MonitoringPorfileName into grp
select new
{
type = "column",
name = grp.Key,
data = (from h in Hours
let gd = grp.Where(x => x.Hours == h)
select gd.Sum(x => x.Count)).ToArray()
}).ToList();
}
}
How can i make this variable columnSeries a global variable? i have searched a lot on this, found list dynamic> new{}; but none of them are working so help will be really appreciated

Make the anonymous type you want a class.
public class ColumnSeries
{
public string type {get; set;}
//...
}
//class level variable
IEnumerable<ColumnSeries> columnSeries = null;
//then create the ColumnSeries list
columnSeries = (from l in logs
group l by l.MonitoringPorfileName into grp
select new ColumnSeries
{
type = "column",
name = grp.Key,
data = (from h in Hours
let gd = grp.Where(x => x.Hours == h)
select gd.Sum(x => x.Count)).ToArray()
});

You are creating an anonymous type inside your if statment but you want to use the result ouside the scope of the if statement. Usually you just define 'columnSeries' before the if BUT this is an anonymous type so it's not obvous.
So, before the if statement do the following (untested but should be close):
var columnSeries = Enumerable.Repeat(new {type="", name="", data=new int[]{0}}, 0);
Check out this question for more info

Related

Convert SQL query into linq C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Convert SQL query to LINQ C#
SELECT ts.TeacherId, count(Distinct ts.SubjectId) as Subjects
from dbo.TeacherSubjects ts
GROUP BY ts.TeacherId
HAVING ts.TeacherId = 2352
here is my LINQ query
var SubjectsGroup = db.TeacherSubjects.Where(p => p.TeacherId == 2352).Distinct().GroupBy(x => x.TeacherId);
var teacherId = ; // your teacherId here
var result = new {
TeacherId = teacherId,
Subjects = db.TeacherSubjects.Where(ts => ts.TeacherId == teacherId).Select(x => x.SubjectId).Distinct().Count()
};
Or you can group by SubjectId:
var teacherId = ; // put your teacherId here
var result =
(from ts in TeacherSubjects.Where(x => x.TeacherId == teacherId)
group ts by ts.SubjectId into gr
select new
{
TeacherId = teacherId,
Subjects = gr.Count()
}).ToList();

Group by custom list linq c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have problem in understanding custom group by and order by. Here is the scenario,
I have an object contain data.
Objects->object->name; (structure of the object)
and i want to group by custom list that i have (name is the key object for grouping).
name is like a,b,c and i want b,c,a(sample).
Here is my work around
IOrderedEnumerable<IGrouping<string, Snit>> assetGrouping = sUnits.ToArray().GroupBy(e => e.Name).OrderBy(e => e.Key);
and i want to group and order by my custom list
List<string > customOrder=new List<string>();
customOrder.Add("any");
customOrder.Add("some");
can any body help me..
This is what I scrapped using LINQPad:
Prerequisites:
public class Data
{
public string Name {get; set;}
}
var keys = new[]{"1", "15", "13", "16"};
var random = new Random();
var data = Enumerable.Range(1, 100)
.Select( _ => new Data
{
Name = random.Next(24).ToString()
});
var keys = new[]{"1", "15", "13", "16"};
Now, the grouping:
var grouped = data.GroupBy(x => keys.FirstOrDefault(k=>k==x.Name));
The query above will group the items in data as following: for each value x in data, if there is a key k in keys with k == x.Name, the value will be added to a separate group having the key k; all the other values will be added to a separate group having the key null;
For the ordering of the groups you just need to use the position of each group key in the keys array.
var ordered = grouped.OrderBy( g =>
{
var index = Array.IndexOf(keys, g.Key);
return index == -1 ? int.MaxValue : index;
});
Array.IndexOf will return -1 if the item wasn't found in the dictionary. In this case, according to your needs, the item must be at the end of the collection so return max value. Otherwise, just use the index of the key.
The results are in the image below:

How to get Count of value of nested object by linq [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone please suggest how to get the sum of points from all the Event objects by using linq.
new Category
{
CategoryId = "NUTR",
CategoryName = "NUTR",
CategoryDescription = "NUTR Desc",
TotalPoints = "100",
Events = new List<Event>
{
new Event{Firstname = "HELEN",Surname = "BECKETT",
Points = "10", Description = "NUTR Desc",Eventdate = "2013/04/19",EntityNumber = "1203206956"},
new Event{Firstname = "PAUL",Surname = "BECKETT",
Points = "90", Description = "NUTR Desc",Eventdate = "2013/06/19",EntityNumber = "1203206957"}
}
}
You need to use SelectMany to flatten the list of events, then do the sum:
var totalPoints = categories
.SelectMany(c => c.Events)
.Sum(e => Convert.ToInt32(e.Points));
If I correclty understand the question this can be achieved by Linq
I suggest you to change the type of Points from string to int if possible
Category cat = ....
var countEvents = cat.Sum(i=>i.Events.Sum(ii=>ii.Points));

populating IEnumerable collection with a List<string> data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an existing List that countains data
List<string>countries
I want to somehow loop through this data an populate the following collection
IEnumerable<SelectListItem> Countries
How would I go about in doing this?
The simplest method I can think off is using a Linq query:
Countries = countries.Select(country => new SelectListItem {Text = country, Value = country});
You won't have the ability to differ the text from the value, however.
Presuming this is related to mvc... try this
IEnumerable<SelectListItem> Countries = countries.Select(c => new SelectListItem { Text = c, Value = c });
Something like this I think.
IEnumerable<SelectListItem> Countries = countries.Select(c => new SelectListItem
{
Text = c,
Value = c
});

List<object> compare [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have searched for a few examples and not found one that is similar to what I have and what I want to achieve.
I have 2 lists
class object1
{
string obj1_name;
int obj1_qty;
}
List<object1> listA
class object2
{
string obj2_name;
int obj2_qty;
}
List<object2> listB;
Now, using ListA as the primary list I want to see if ListB contains an object with the same name and if so, what is the quantity and hence, does the obj1_qty = obj2_qty each other? if not, there is a difference and I need to show it, most likely in a 3rd list which would be the difference, of the qty's if they exist. Note, ListA can be bigger or smaller than ListB
Show All List A (master list contains all objects)
Show the difference between those names/qty's that exist in both lists.
Gracias
Souncs like a join would work for you:
var query = from a in listA
join b in listB on a.obj1_name equals b.obj2_name
where a.obj1_qty != b.obj2_qty
select new {
Name = a.obj1_name,
QtyA = a.obj1_qty,
QtyB = b.obj2_qty,
Diff = a.obj1_qty - b.obj2_qty
};
The simpelest way to do this is just use a join from Linq
var items = from l1 in listA
join l2tmp1 in listB on l1.obj1_name equals l2.obj2_name into l2tmp2
from l2 in l2tmp2.DefaultIfEmpty();
select new {
ItemA = l1,
ItemB = l2,
Name = l1.obj1_name,
Difference = (l2 == null) ? 0 : l1.ob1_qty - l2.ob2_qty
};
items will now hold a IEnumerable of a anonymous class that holds the a reference to the item in ListA, a reference to the item in listB, the matching name, and the difference.

Categories

Resources