2013-03-21 85 views
0

我試圖使用推薦LLBLGEN語法查詢投影(http://www.llblgen.com/documentation/3.5/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Adapter/gencode_usingentityview_adapter.htm#projectionsLINQ到對象查詢LLBLGEN投影

IEntityView2 view = table.DefaultView; 
List<A1AllocationHelp1TableDTO> something = 
    (from c in view 
    select new A1AllocationHelp1TableDTO 
    { 
     RecordStatus = c.RecordStatus, 
     UniqueIdent = c.UniqueIdent 
    }).ToList(); 

但我發現了關於「選擇」這個錯誤:

The type arguments for method 'IEnumerable<TResult> 
System.Linq.Enumerable.Select<TSource, TResult>(this IEnumerable<TSource>, 
Func<TSource, TResult>)' cannot be inferred from the query. 

搞笑的是,同樣的作品只是在VB.Net罰款

Dim view As IEntityView2 = table.DefaultView 
Dim something As List(Of A1AllocationHelp1TableDTO) = _ 
    (From c In view 
    Select New A1AllocationHelp1TableDTO With _ 
      { 
       .RecordStatus = c.RecordStatus, _ 
       .UniqueIdent = c.UniqueIdent 
      }).ToList() 

我使用VS2010,.NET 4和LLBLGen 2.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); 
} 
+0

請顯示IEntityView2的定義。 – 2013-03-21 08:28:10

+0

我編輯添加定義 – kooshka 2013-03-21 08:45:11

回答

2

IEntityView2繼承了非通用接口IEnumerable。然而Select方法需要通用版本。這就是爲什麼你會收到錯誤。

假設你想打開的屬性上IEntity2定義,下面的工作:

view.Cast<IEntity2>() 
    .Select(c => new A1AllocationHelp1TableDTO 
      { 
       RecordStatus = c.RecordStatus, 
       UniqueIdent = c.UniqueIdent 
      }) 
    .ToList(); 

它工作在VB.NET,因爲它使用後期綁定。你可以很容易地看到這在下面的示例:

Dim view As IEntityView2 = table.DefaultView 
Dim something As List(Of A1AllocationHelp1TableDTO) = _ 
(From c In view 
Select New A1AllocationHelp1TableDTO With _ 
     { 
      .RecordStatus = c.IDontExist _ 
     }).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) 
+0

演員實際上是查看.Cast ()。選擇(c => ...但這正是我需要的 – kooshka 2013-03-21 09:00:56

+0

@kooshka:但爲什麼選擇呢?如果'view'中的項目已經是目標對象,您只需要這樣做:'view.Cast ()。ToList();'。不需要創建新對象 – 2013-03-21 09:02:43

+0

我的不好。意思是Cast 。view包含A1AllocationHelp1Entity的實例,它僅在運行時定義,因爲這是在WCF中,因此我需要返回一個靜態定義的DTO – kooshka 2013-03-21 09:11:34

0

.DefaultView返回一個類型的視圖,它實現IEntityView2,但你不應該將其轉換爲IEntityView2,因爲你那麼失去了泛型類型。所以你應該這樣做:

List<A1AllocationHelp1TableDTO> something = 
    (from c in table.DefaultView 
    select new A1AllocationHelp1TableDTO 
    { 
     RecordStatus = c.RecordStatus, 
     UniqueIdent = c.UniqueIdent 
    }).ToList(); 

這樣,編譯器知道視圖的泛型類型。

+0

謝謝Frans,但那也行不通,我仍然得到相同的錯誤。表的類型是IEntityCollection2 – kooshka 2013-03-21 09:20:47