我想我理解从匿名类型返回记录但是在这里我想创建新的CatalogEntries,并从所选的值设置它们.(context是一个Devart LinqConnect数据库上下文,它可以让我抓取一个视图).
我的解决方案有效,但看起来很笨拙.我想在一个声明中做到这一点.
var query = from it in context.Viewbostons
select it;
foreach (GPLContext.Viewboston item in query)
{
CatalogEntry card = new CatalogEntry();
card.idx = item.Idx;
card.product = item.Product;
card.size = (long)item.SizeBytes;
card.date = item.Date.ToString();
card.type = item.Type;
card.classification = item.Classification;
card.distributor = item.Distributor;
card.egplDate = item.EgplDate.ToString();
card.classificationVal = (int)item.ClassificationInt;
card.handling = item.Handling;
card.creator = item.Creator;
card.datum = item.Datum;
card.elevation = (int)item.ElevationFt;
card.description = item.Description;
card.dirLocation = item.DoLocation;
card.bbox = item.Bbox;
card.uniqID = item.UniqId;
values.Add(card);
}
CatalogResults response = new CatalogResults();
Run Code Online (Sandbox Code Playgroud)
我刚试过这个:
var query2 = from item in context.Viewbostons
select new CatalogResults
{ item.Idx,
item.Product,
(long)item.SizeBytes,
item.Date.ToString(),
item.Type,
item.Classification,
item.Distributor,
item.EgplDate.ToString(),
(int)item.ClassificationInt,
item.Handling,
item.Creator,
item.Datum,
(int)item.ElevationFt,
item.Description,
item.DoLocation,
item.Bbox,
item.UniqId
};
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
错误79无法使用集合初始值设定项初始化类型'CatalogService.CatalogResults',因为它未实现'System.Collections.IEnumerable'C:\ Users\ysg4206\Documents\Visual Studio 2010\Projects\CatalogService\CatalogService\CatalogService.svc.cs 91 25 CatalogService
我应该告诉你我想要返回的CatalogResults的定义是什么:
[DataContract]
public class CatalogResults
{
CatalogEntry[] _results;
[DataMember]
public CatalogEntry[] results
{
get { return _results; }
set { _results = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
今天我的思绪沉闷,向所有人道歉.你很乐于助人.最终结果将由WCF序列化为JSON结构,我需要包含在对象中的数组,其中包含有关大小等的一些信息.
由于.NET 3.0您可以使用如下所示的对象初始值设定项:
var catalogResults = new CatalogResults
{
results = context.Viewbostons
.Select(it => new CatalogEntry
{
idx = it.Idx,
product = it.Product,
...
})
.ToArray()
};
Run Code Online (Sandbox Code Playgroud)
因此,如果这只是您使用CatalogEntry属性设置器的地方 - 将所有属性设置为只读,那么CatalogEntry将是不可变的.
MSDN,对象初始化器:
通过对象初始值设定项,您可以在创建时将值分配给对象的任何可访问字段或属性,而无需显式调用构造函数.
| 归档时间: |
|
| 查看次数: |
286 次 |
| 最近记录: |