简体   繁体   English

将linq结果集转换为数据表

[英]Converting linq result set to data table

var query = (from row in playerSpotAndValue.AsEnumerable()
             where !row.Field<string>("RunningTotal").Contains("/")
             group row by row.Field<UInt16>("SpotID") into spotID
             orderby spotID.Key
             select new
             {
                 SpotID = spotID.Key,
                 HandTotal= spotID.Max(p => Convert.ToUInt16(p.Field<string>("RunningTotal"))),
                 RowsCount = spotID.Count()
             }); 

How to use CopyToDataTable in this query so that i can query like this 如何在此查询中使用CopyToDataTable,以便我可以这样查询

query.CopyToDataTable().Rows[0]["HandTotal"].ToString();

Yes, you need to use the following extension method : 是的,您需要使用以下扩展方法

public static DataTable CopyToDataTable<T>(this IEnumerable<T> source) where T : DataRow

But first you need to have IEnumerable<DataRow> but not of anonymous types. 但首先你需要IEnumerable<DataRow>而不是匿名类型。

Your table should have the columns you want to populate, then use DateTable.NewRow() method 您的表应该包含要填充的列,然后使用DateTable.NewRow()方法

Assuming that dtDealerTotal (orgininally referenced in your question) is the table in which you want to find the rows with SpotID from your query and you want to add the HandTotal result as DataColumn : 假设dtDealerTotal (在您的问题中已经引用)是您想要在查询中查找具有SpotID的行的表,并且您希望将HandTotal结果添加为DataColumn

DataTable result = dtDealerTotal.Clone();
result.Columns.Add(new DataColumn("HandTotal", typeof(UInt16)));
var dealers = dtDealerTotal.AsEnumerable();
foreach (var x in query)
{
    foreach (var d in dealers.Where(dr => dr.Field<UInt16>("SpotID") == x.SpotID))
    {
        var fields = d.ItemArray;
        fields.Concat(new Object[] { x.HandTotal });
        result.Rows.Add(fields);
    }
}

Note : You can only use CopyToDataTable on existing DataRows, not on anonymous types. 注意 :您只能在现有DataRows上使用CopyToDataTable ,而不能在匿名类型上使用。 So you either need to select these rows first to copy them into a new DataTable or (when the schema is different like here, since you want to add HandTotal ) you need to create a new DataTable manually, which is what i did. 因此,您需要首先选择这些行以将它们复制到新的DataTable (或者当模式与此处不同时,因为您要添加HandTotal ),您需要手动创建新的DataTable ,这就是我所做的。

This is a way you can achieve, what you want . 这是一种你可以实现的方式,你想要的。

Your LINQ query, and here is a function that return DataTable: 您的LINQ查询,这是一个返回DataTable的函数:

public static DataTable ToDataTable(DataContext ctx, object query) // ctx is your data context, & query is your linq query 
{
    IDbCommand cmd;

    try
    {
        cmd = ctx.GetCommand(query as IQueryable);
    }
    catch
    {
        throw new Exception("Could not connect database");
    }

    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = (SqlCommand)cmd;
    DataTable dt = new DataTable();

    try
    {
        cmd.Connection.Open();

    }
    catch
    {
        cmd.Connection.Close();
        throw new Exception("Could not connect database");
    }

    try
    {
        adapter.Fill(dt);
        cmd.Connection.Close();
    }
    catch(Exception ex)
    {
        cmd.Connection.Close();
        throw new Exception("Query Parsing Error");
    }

    return dt;
}
query.First().HandTotal.ToString() 

This way I can find hand total in very first row and therefore I need not put my linq query result set in data table. 这样我就可以在第一行找到hand total,因此我不需要将我的linq查询结果集放在数据表中。 I would like to say thank to Mr. kmp. 我要感谢kmp先生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM