English 中文(简体)
LINQ - 是否有办法在不按要求分类后获得要素价值?
原标题:LINQ - Is there a way to get element values after grouping without ForEach?

我有一个项目表,包含一个类别领域。 这是科索沃联邦统计局。 我想把我的项目按类别分类,并归还项目和类别。 姓名。

我知道,我可以在C#中这样做:

var ItemsByCat = 
    from i in Items
    group i by i.CategoryId into g
    select g

foreach(var n in ItemsByCat)
{
    blah, blah
}

我的问题是——是否有办法实现分组的部分要素价值,而不必通过 lo子来消化? 类似:

from i in Items
group i by i.CategoryId into g
select new {
    CategoryID = g.Key,
    CategoryName = ???,
    Items = ???
}

非常感谢

BK

最佳回答

我认为,这是你回顾的:

from i in Items
group i by i.CategoryId into g
select new 
{
    CategoryID = g.Key,
    CategoryName = g.First().Category.Name,
    Items = g
}

EDIT:我希望从评论中回答你的问题。

g is a collection of Items grouped by their CategoryId. Items in the select can be a projection including whatever subset of Item properties you require. Example:

from i in Items
group i by i.CategoryId into g
select new 
{
    CategoryID = g.Key,
    CategoryName = g.First().Category.Name,
    Items = g.Select(a => new{a.ItemID, a.ItemName})
}
问题回答

加入类别表有何意义?

from i in Items
join c in Categories on i.CategoryId equals c.Id
group i by i.CategoryId into g
select new {
    CategoryID = g.Key,
    CategoryName = c.Name,
    Items = i
}

应为:

given you have the relationship setup properly.

The items should be the values of group by, not sure why you d need to do it again.

Assuming you have a collection of Categories in a variable categories it would look like this:

var ItemsByCategory = 
  from i in items
  join c in categories on i.CategoryId equals c.Id
  group i by i.CategoryId into g
  select new {
    CategoryId=g.Key,
    CategoryName=c.Name,
    Items=g.Items
}




相关问题
Mysql Foreach Child show Max() and Min() in a single line

Is it possible to use a select statement so that it returns Max and min columns of a child row foreach Parent record? So foreach parent record that has MANY child records I want to see the Max and ...

Foreach in SQL?

I m not quite sure how to do this in SQL. Here it is in pseudocode: Take the list of nodes with content type X. For each node, take the value of field Y. Insert into term_nodes VALUES ((tid that ...

Custom container requirement to work with Qt s foreach

What is the bare minimum amount of code to create a custom container that would work with Qt foreach macro? I have this so far template< class T > class MyList { public: class iterator { ...

foreach loops & stdclass objects

I ve seen similar questions on here but I can t seem to apply the solutions to my problem. I have a variable called $results which I got from an API. I ll change the proper nouns so as to protect my ...

PHP - Foreach loops and ressources

I m using a foreach loop to process a large set of items, unfortunately it s using alot of memory. (probably because It s doing a copy of the array). Apparently there is a way to save some memory with ...

Hide a column programmatically in MS-Access

I want to hide or show a column based on variable data from a users selection. How do you set a column to hidden in MS-Access 2003? For Example, After user change event... For Each ctl In Me....

iterating over map and array simultaneously in a for loop

I am having some trouble creating a for loop within a constructor to iterate over a map and an array at the same time. Here, it is indicated that this cannot be done with an enhanced for loop. I have ...

Changing a struct inside another struct in a foreach loop

The following code prints (when invoking MyMethod): 0 0 0 1 I would expect it to print: 0 0 1 1 Why is this? Code: private struct MyStruct { public MyInnerStruct innerStruct; } private ...

热门标签