[英]How to split variable from linq query result
I want to split the data result from linq query , let's say from this code where I write我想从 linq 查询中拆分数据结果,让我们从我编写的这段代码中说出来
string[] items;
var data = from c in db.CMRTables where c.Id.Equals(17776) select new { c.Auth, c.amount };
string s = String.Join(",", data);
items = s.Split(',');
MessageBox.Show(items[0]);
It will return { Auth = 80231252 , amount = 8888888 } , I want to split the value so I can store it to variable ,它将返回 { Auth = 80231252 , amount = 8888888 } ,我想拆分该值以便将其存储到变量中,
My expected result is 80231252 and 8888888 only , when I split it , how to do it?我的预期结果只有 80231252 和 8888888 ,当我拆分它时,该怎么做? and what I select is basically not 2 values , but more than 2 values or I can say like 19 values
我选择的基本上不是 2 个值,而是 2 个以上的值,或者我可以说 19 个值
Why cant you just,你为什么不能,
var data = (from c in db.CMRTables where c.Id.Equals(17776) select new { c.Auth, c.amount }).FirstOrDefault();
var ExpectedResult = data.Auth + "," + data.amount;
Split on ',', then use enumerator to process each entry:在 ',' 上拆分,然后使用 enumerator 处理每个条目:
string[] items = s.Split(',')
.Select(keyVal => keyVal.Split('=')[1].Trim())
.ToArray();
This above:上面这个:
splits string s
using separator ','
into temporary array ( Split()
)使用分隔符
','
将字符串s
拆分为临时数组 ( Split()
)
for each item in this temporary array gets this item as keyVal
string and process those using formula on right side of '=>'
( Select()
)对于此临时数组中的每个项目,将此项目作为
keyVal
字符串并使用'=>'
右侧的公式处理这些项目( Select()
)
converts the result to string[]
array.将结果转换为
string[]
数组。
You could use Regex for the purpose.您可以为此目的使用正则表达式。
var numbers = Regex.Matches(str, @"\d+")
.Cast<Match>()
.Select(x=>x.Value);
If you want the result as List<Long>
如果您希望结果为
List<Long>
var numbers = Regex.Matches(str, @"\d+")
.Cast<Match>()
.Select(x=>long.Parse(x.Value));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.