LINQ:如何在分组时选择多个List() [英] LINQ: How do I select many into a List(), while Grouping

查看:102
本文介绍了LINQ:如何在分组时选择多个List()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我对这个LINQ语句有困难:

  class  clsFilterNode 
{
public string 值{ get ; set ; }
public string 文字{ get ; set ; }
public List< clsFilterNode> FilterNodes { get ; set ; }

public clsFilterNode( string strValue,字符串 strText)
{
Value = strValue;
Text = strText;
FilterNodes = new 列表< clsFilterNode>();
}
}

// List< ResourceObject>
Resource = RetrieveMasterSet();

lstChildNodeItems =资源
。其中(objWhere = > objWhere.ResourceStatusParentID == 256
.GroupBy(objGroup = > objGroup.ResourceStatusID)
.Select(objSelect = > new
clsFilterNode(objSelect.FirstOrDefault()。ResourceStatusID.ToString(),
objSelect。 FirstOrDefault()。ResourceStatus))。ToList();



资源是一个巨大的List< resourceobject> (不是clsFilterNode的集合),我正在尝试根据此记录集中的不同记录填充TreeView进行过滤。 LINQ语句背后的一般想法是我需要一个不同的资源状态列表,其中状态父ID是给定的数字(在这种情况下是256)。



可能有人请帮我解决这个问题?还是不可能?我正在尝试避免SQL,而是立即获取所有内容并在代码中处理它,因为用户与SQL数据库的连接可能很慢。我知道在SQL中会有多容易,但是如果可能的话,这种方法是首选。



问候,

Tom。

解决方案

我认为你遇到的问题是流利的Select()。当你像这样使用它时,你可以认为它有点像ForEach,并且传递给匿名函数的参数是一个对象T,其中枚举项是IEnumerable< T> (根据您的代码注释,T似乎是ResourceObject)。



您将anon函数参数视为IEnumerable,这可能会破坏构建代码。修正后的版本如下:



 lstChildNodeItems =资源
.Where(objWhere = > objWhere.ResourceStatusParentID == 256
.GroupBy(objGroup = > ; objGroup.ResourceStatusID)
。选择(objSelect = > new
clsFilterNode(objSelect.ResourceStatusID.ToString(),
objSelect.ResourceStatus))。ToList();





几个笔记:

除非你打算需要List< T>功能,比如Add()或Remove(),在这个方法之后,我会建议使用.ToArray(),因为它的触摸更灵活,更小。



Resharper [ ^ ]是一个很好的工具,可以在编写代码时找到这些东西;它会在编写代码时自动指出这种类型不匹配。我不能推荐它。


试试这个

  var  selectedItems = 来自  Resource.Where(objWhere = > ;  objWhere.ResourceStatusParentID ==  256  group  rows by rows.ResourceStatusID 
into 条记录选择 new
{clsFilterNode(records.FirstOrDefault()。ResourceStatusID.ToString(),
records.FirstOrDefault()。ResourceStatus.ToString())
} .ToList();


Hi All,

I'm having difficulties with this LINQ statement:

  class clsFilterNode
  {
    public string Value { get; set; }
    public string Text { get; set; }
    public List<clsFilterNode> FilterNodes { get; set; }

    public clsFilterNode(string strValue, string strText)
    {
      Value = strValue;
      Text = strText;
      FilterNodes = new List<clsFilterNode>();
    }
  }

//List<ResourceObject>
Resource = RetrieveMasterSet();

lstChildNodeItems = Resource
    .Where(objWhere => objWhere.ResourceStatusParentID == 256)
    .GroupBy(objGroup => objGroup.ResourceStatusID)
    .Select(objSelect => new  
        clsFilterNode(objSelect.FirstOrDefault().ResourceStatusID.ToString(),
        objSelect.FirstOrDefault().ResourceStatus)).ToList();


Resource is a huge List<resourceobject> (not a collection of clsFilterNode), and I'm trying to fill a TreeView based on the distinct records within this recordset for filtering. The general idea behind the LINQ statement is I need a List of distinct Resource Statuses where the Status Parent ID is a given number (in this case 256).

Could someone help me out with this please? Or is it not possible? I'm trying to avoid SQL, and instead get everything at once and deal with it in code because the users connection to the SQL database could be slow. I do understand how much easier it would be in SQL but this method is preferred if it is possible.

Regards,
Tom.

解决方案

I think the issue that you're having is with the fluent Select(). When you use it like this, you can think of it sort of like a ForEach, and the parameter passed to the anonymous function is an object T where the enumerated item is IEnumerable<T> (T appears to be ResourceObject based on you code comments).

You're treating the anon function parameter as an IEnumerable, which is likely breaking the code on build. A corrected version would look like:

lstChildNodeItems = Resource
    .Where(objWhere => objWhere.ResourceStatusParentID == 256)
    .GroupBy(objGroup => objGroup.ResourceStatusID)
    .Select(objSelect => new  
        clsFilterNode(objSelect.ResourceStatusID.ToString(),
        objSelect.ResourceStatus)).ToList();



A couple of notes:
Unless you plan to need List<T> functionality, such as Add() or Remove(), after this method then I would suggest using .ToArray() instead, as it's a touch more flexible and smaller.

Resharper [^]is a great tool to find this stuff while writing your code; it will automatically point out this sort of type mismatch while writing code. I cannot recommend it enough.


Try this

var selectedItems = from rows in Resource.Where(objWhere => objWhere.ResourceStatusParentID == 256) group rows by rows.ResourceStatusID
into records select new
{ clsFilterNode(records.FirstOrDefault().ResourceStatusID.ToString(),
   records.FirstOrDefault().ResourceStatus.ToString())
}.ToList();


这篇关于LINQ:如何在分组时选择多个List()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆