C# Linq to对象查询LLBLGen投影

C# Linq to对象查询LLBLGen投影,c#,linq-to-objects,llblgenpro,C#,Linq To Objects,Llblgenpro,我正在尝试使用推荐的LLBLGen语法来查询投影() 我正在使用VS2010、.NET4和LLBLGEN2.6。 我想不出怎么解决这个问题有人能帮我一下吗 谢谢 编辑: IEntityView2由LLBLGen生成,这是它的定义 public interface IEntityView2 : IEnumerable { bool AllowEdit { get; set; } bool AllowNew { get; set; } bool AllowRemove { g

我正在尝试使用推荐的LLBLGen语法来查询投影()

我正在使用VS2010、.NET4和LLBLGEN2.6。 我想不出怎么解决这个问题有人能帮我一下吗

谢谢

编辑:

IEntityView2由LLBLGen生成,这是它的定义

public interface IEntityView2 : IEnumerable
{
    bool AllowEdit { get; set; }
    bool AllowNew { get; set; }
    bool AllowRemove { get; set; }
    int Count { get; }
    PostCollectionChangeAction DataChangeAction { get; set; }
    IPredicate Filter { get; set; }
    IEntityCollection2 RelatedCollection { get; }
    ISortExpression Sorter { get; set; }
    IEntity2 this[int index] { get; }
    event ListChangedEventHandler ListChanged;
    bool Contains(IEntity2 value);
    void CreateProjection(List<IEntityPropertyProjector> propertyProjectors, DataTable destination);
    void CreateProjection(List<IEntityPropertyProjector> propertyProjectors, IEntityCollection2 destination);
    void CreateProjection(List<IEntityPropertyProjector> propertyProjectors, IEntityDataProjector projector);
    void CreateProjection(List<IEntityPropertyProjector> propertyProjectors, DataTable destination, bool allowDuplicates);
    void CreateProjection(List<IEntityPropertyProjector> propertyProjectors, IEntityCollection2 destination, bool allowDuplicates);
    void CreateProjection(List<IEntityPropertyProjector> propertyProjectors, IEntityDataProjector projector, bool allowDuplicates);
    void CreateProjection(List<IEntityPropertyProjector> propertyProjectors, DataTable destination, bool allowDuplicates, IPredicate filter);
    void CreateProjection(List<IEntityPropertyProjector> propertyProjectors, IEntityCollection2 destination, bool allowDuplicates, IPredicate filter);
    void CreateProjection(List<IEntityPropertyProjector> propertyProjectors, IEntityDataProjector projector, bool allowDuplicates, IPredicate filter);
    int IndexOf(IEntity2 value);
    IEntityCollection2 ToEntityCollection();
    IEntityCollection2 ToEntityCollection(int startIndex);
}
公共接口IEntityView2:IEnumerable
{
布尔允许它{get;set;}
bool AllowNew{get;set;}
bool AllowRemove{get;set;}
整数计数{get;}
PostCollectionChangeAction数据更改操作{get;set;}
IPredicate筛选器{get;set;}
IEntityCollection2 RelatedCollection{get;}
ISortExpression分类器{get;set;}
IEntity2此[int索引]{get;}
事件ListChangedEventHandler ListChanged;
bool包含(IEntity2值);
void CreateProjection(列出propertyProjectors、数据表目标);
void CreateProjection(列出propertyProjectors、IEntityCollection2目标);
void CreateProjection(列出propertyProjectors、IEntityDataProjector);
void CreateProjection(列出propertyProjectors、DataTable destination、bool allowDuplicates);
void CreateProjection(列出propertyProjectors、IEntityCollection2目标、bool allowDuplicates);
void CreateProjection(列出propertyProjectors、IEntityDataProjector投影仪、bool allowDuplicates);
void CreateProjection(列出propertyProjectors、DataTable destination、bool allowDuplicates、IPredicate筛选器);
void CreateProjection(列出propertyProjectors、IEntityCollection2目标、bool allowDuplicates、IPredicate筛选器);
void CreateProjection(列出propertyProjectors、IEntityDataProjector、bool allowDuplicates、IPredicate筛选器);
int IndexOf(IEntity2值);
EntityCollection 2到EntityCollection();
IENTITY集合2至实体集合(int startIndex);
}

IEntityView2
继承非通用接口
IEnumerable
。但是,
Select
方法需要通用版本。这就是为什么会出现错误

假设要访问的属性是在
IEntity2
上定义的,则以下操作将起作用:

view.Cast<IEntity2>()
    .Select(c => new A1AllocationHelp1TableDTO
           {
               RecordStatus = c.RecordStatus,
               UniqueIdent = c.UniqueIdent
           })
    .ToList();
我使用的属性不存在(
IDontExist
)。此代码仍将编译,但在运行时引发异常:

MissingMemberException: Public member 'IDontExist' on type 'IEntity2' not found.
   at Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String& MemberName, Boolean ReportErrors)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 

.DefaultView返回一个类型化视图,该视图实现了IEntityView2,但不应将其强制转换为IEntityView2,因为这样会丢失泛型类型。所以你应该这样做:

List<A1AllocationHelp1TableDTO> something = 
    (from c in table.DefaultView
     select new A1AllocationHelp1TableDTO
     {
         RecordStatus = c.RecordStatus,
         UniqueIdent = c.UniqueIdent
     }).ToList();
列出一些东西=
(来自table.DefaultView中的c)
选择新建A1AllocationHelp1TableDTO
{
RecordStatus=c.RecordStatus,
UniqueIdent=c.UniqueIdent
}).ToList();

这样,编译器就知道视图的泛型类型

请显示
IEntityView2
的定义。我已经编辑添加了cast实际要查看的定义。cast()。选择(c=>…但这正是我想要的needed@kooshka:但是为什么要选择呢?如果
视图中的项已经是目标对象的项,则只需执行以下操作:
view.Cast().ToList();
。不需要创建新对象。我的不好。我的意思是Cast.view保存一个AllocationHelp1实体的实例,该实体仅在运行时定义,因为它在WCF中,所以我需要返回一个静态定义的DTOThanks Frans,但这也不起作用,我仍然会收到相同的错误。表的类型是IEntityCollection2
view.Cast<IEntity2>()
    .Select(c => new A1AllocationHelp1TableDTO
           {
               RecordStatus = c.RecordStatus,
               UniqueIdent = c.UniqueIdent
           })
    .ToList();
Dim view As IEntityView2 = table.DefaultView
Dim something As List(Of A1AllocationHelp1TableDTO) = _
(From c In view
 Select New A1AllocationHelp1TableDTO With _
      {
          .RecordStatus = c.IDontExist _
      }).ToList()
MissingMemberException: Public member 'IDontExist' on type 'IEntity2' not found.
   at Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String& MemberName, Boolean ReportErrors)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
List<A1AllocationHelp1TableDTO> something = 
    (from c in table.DefaultView
     select new A1AllocationHelp1TableDTO
     {
         RecordStatus = c.RecordStatus,
         UniqueIdent = c.UniqueIdent
     }).ToList();