从LINQ创建对象 [英] create object from LINQ

查看:110
本文介绍了从LINQ创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象:

public class DataItem
{

    public string Location
    {
        get;
        set;
    }

    public List<string> PersonList
    {
        get;
        set;
    }
}

我从表中得到了一些返回如下结果的结果:

I have some results from a table that return something like:

Room1 John
Room1 Jim
Room1 Dawn
Room1 Bob
Room1 Katie

我写了一些LINQ:

var grouped = from table in sqlResults.AsEnumerable()
              group table by new { placeCol = table["LOCATION"] } into groupby
              select new
              {
                  Value = groupby.Key,
                  ColumnValues = groupby
              };

将我的结果分组...但是我想将其放入我的对象(DataItem)中.我已经看到了几个示例,但没有任何效果……我想念什么?

Which groups my results... But I'd like to put this into my object (DataItem). I've seen a couple of examples but nothing has worked... What am I missing?

推荐答案

  1. 不要在一个新的匿名对象上进行分组,而该匿名对象只有一个代表您的位置的值,而仅在位置上进行分组

  1. Don't group on a new anonymous object with a single value representing your location, just group on the location

不要选择新的匿名对象作为结果,请选择您关心的对象

Don't select a new anonymous object as the result, select the object you care about

获取人员列表时,从组中选择人员名称.

Select out the person name from the group when getting the person list.


var grouped = from row in sqlResults.AsEnumerable()
                group row by row.Field<string>("LOCATION") into groupby
                select new DataItem()
                {
                    Location = groupby.Key,
                    PersonList = groupby.Select(row => 
                        row.Field<string>("Person")).ToList();
                };

这篇关于从LINQ创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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