我有一个包含一个nvarchar列这样一个数据库表:
I have a database table that contains an nvarchar column like this:
1|12.6|18|19
我有一个业务对象,有一个十进制数[]属性。
I have a Business Object that has a Decimal[] property.
我的LINQ查询看起来是这样的:
My LINQ Query looks like this:
var temp = from r in db.SomeTable select new BusinessObject {
// Other BusinessObject Properties snipped as they are straight 1:1
MeterValues = r.MeterValues.Split('|').Select(Decimal.Parse).ToArray()
};
var result = temp.ToArray();
这将引发NotSupportedException异常:法'System.String []斯普利特(字符[])没有支持转换为SQL
This throws an NotSupportedException: Method 'System.String[] Split(Char[])' has no supported translation to SQL.
这有点儿吮吸:)有没有什么办法可以做到这一点,而不必一个字符串属性添加到业务对象或选择匿名类型,然后通过它迭代?
That kinda sucks :) Is there any way I can do this without having to add a string property to the business object or selecting an anonymous type and then iterating through it?
我目前的解决方案是:
var temp = from r in db.SomeTable select new {
mv = r.MeterValues,
bo = new BusinessObject { // all the other fields }
};
var result = new List<BusinessObject>();
foreach(var t in temp) {
var bo = t.bo;
bo.MeterValues = t.mv.Split('|').Select(Decimal.Parse).ToArray();
result.Add(bo);
}
return result.ToArray(); // The Method returns BusinessObject[]
这是有点难看,虽然,与该临时表。
That's kinda ugly though, with that temporary list.
我试着加入让MV = r.MeterValues.Split(|),选择(Decimal.Parse).ToArray()
但是,基本上导致到相同NotSupportedException异常
I've tried adding a let mv = r.MeterValues.Split('|').Select(Decimal.Parse).ToArray()
but that essentially leads to the same NotSupportedException.
这是.NET 3.5SP1如果该事项。
This is .net 3.5SP1 if that matters.
您需要强制选择
子句通过调用客户端上运行 .AsEnumerable()
第一:
You need to force the select
clause to run on the client by calling .AsEnumerable()
first:
var result = db.SomeTable.AsEnumerable().Select(r => new BusinessObject {
...
MeterValues = r.MeterValues.Split('|').Select(Decimal.Parse).ToArray()
}).ToList();