简体   繁体   English

Linq 从返回的 var 中获取值

[英]Linq get the value from the var returned

I have the below code I am getting a distinct value from the datatables and then using the value to select on that datatable.我有下面的代码,我从数据表中获得了一个不同的值,然后使用该值在该数据表上进行选择。

string ccId = d.ToString(); 

is being returned as "{ id = B08 }" -- I just need B08 how can I get that value?被返回为“{ id = B08 }”——我只需要 B08 我怎样才能得到那个值?

var distinctIds = dt.AsEnumerable()
                  .Select(s => new { id = s.Field<string>("CCId"),})
                  .Distinct().ToList();



foreach (var d in distinctIds)
{
     string ccId = d.ToString();
     DataTable selectedTable = dt.AsEnumerable()
                            .Where(r => r.Field<string>("CCId") == ccId).CopyToDataTable();
     CreateFile(selectedTable);
}

You are forcing this structure when populating distinctIds .您在填充distinctIds时强制使用此结构。 By removing the anonymous (new) call in your select you will have a list of the values only.通过删除您选择中的匿名(新)调用,您将只拥有一个值列表。

var distinctIds = dt.AsEnumerable()
                    .Select(s => s.Field<string>("CCId"))
                    .Distinct().ToList();

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

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