LinqBindingList NEWB question

LinqBindingList NEWB question

Old forum URL: forums.lhotka.net/forums/t/6975.aspx


DCottle posted on Wednesday, May 20, 2009

I have read chapter 14 on the LinqBindingList object, but I am failing to get it instantiated correctly.

 

Can somebody please give me a sample of code (including using statements) where the LBL is created using a linq query with a BusinessListBase collection?

I have tried this:

using Csla.Linq;

Csla.LinqBindingList<INote> filteredList = from INote item in _notesCollection select item;

But I keep getting a couple of errors on that:

Error 1 'Notes.INotes' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.EnumerableRowCollectionExtensions.Cast<TResult>(System.Data.EnumerableRowCollection)'

Error 2 Instance argument: cannot convert from 'Notes.INotes' to 'System.Data.EnumerableRowCollection' 

MORE DETAILS:

In this example I removed the reference to System.Linq, because with the System.Linq reference I kept getting back a System.Linq.Enum erableQuery...and I cannot figure out how to cast it to a LBL or get back an LBL automatically like people are implying here with the code being posted.

Thanks,

David

JoeFallon1 replied on Wednesday, May 20, 2009

Did you review this thread?
http://forums.lhotka.net/forums/thread/31891.aspx

Also try this syntax:

Dim sortedList As Csla.LinqBindingList(Of MyBO)

Dim query As System.Linq.IOrderedQueryable(Of MyBO) = From x In mMyColl Order By x.Line Ascending, x.Code Descending
sortedList = CType(query, Csla.LinqBindingList(Of MyBO))

DCottle replied on Wednesday, May 20, 2009

I reviewed that thread in depth, and also tried the C# version of your syntax above (I did not have a order by statement, so the return type of the linq statement i changed to IEnumerable...I couldn't get the IOrderedQueryable to work either though):

Csla.LinqBindingList<TransactionNote> sortedList;

System.Collections.Generic.IEnumerable<TransactionNote> query = from TransactionNote item in _notesCollection select item//notice,  i cannot for some reason remove the "select item"...the compiler requires it.

sortedList = (Csla.LinqBindingList<TransactionNote>)query;  //error on this line

Unable to cast object of type 'WhereSelectEnumerableIterator`2[Notes.TransactionNote,Notes.TransactionNote]' to type 'Csla.LinqBindingList`1[Notes.TransactionNote]'.

 

any thoughts?

 

AaronErickson replied on Thursday, May 21, 2009

Out of curiosity (and so I can help - lol)...

what is _notesCollection a type of?

should I assume it is something that derives from BLB?

DCottle replied on Thursday, May 21, 2009

It is of type BLB.  I actually got this to work, but I had to change some of my internals to make it work.

 

Basically, I had several collections of objects that were implementing the same interfaces (INotes, and INote).  If I try to return a LinqBindingList<INote> collection, it fails.  I guess it considers the cast to its interface to be some sort of transformation so the result is no longer an identity projection.  There is more code I could include, but hopefully this gets across my trouble.

This works:  (sorry about double spaced lines.  how do you get rid of those?)

public class GenericNotes : BusinessListBase<GenericNotes, GenericNote>, INotes

{

public object GetFilteredNotes(string NoteType)

{

return  from c in this

where c.NoteType == NoteType

select c; //this actually returns a LinqBindingList<GenericNote> collection

}

This does not work:

public class GenericNotes : BusinessListBase<GenericNotes, GenericNote>, INotes

{

public LinqBindingList<INote>)GetFilteredNotes(string NoteType)

{

var foo = from INote c in this

where c.NoteType == NoteType

select c;

return (LinqBindingList<INote>)foo;

}

 

 

 

 

AaronErickson replied on Thursday, May 21, 2009

Ok - thanks for the clarification.

The latter case will certainly not work - nor am I sure even how it would.

One of the things a LBL will do is maintain a mapping from the destination to the original BLB it came from. In order to effectivley do the mapping, it has to deal with objects of the same type (i.e. when you add to the LBL, for example, it will also add to the BLB it came from).

Just thinking this through, if you have an LBL of I, which an interface T implements, but also U implements, how do I know how I can add that item to the BLB it came from?

There might be an answer to that that I need to think through more, but I can assure that such functionality is well beyond the scope of how we have designed this subsystem to date.

As of now, we support LBL where you get back an LBL with the same child types that your BLB contains.

Copyright (c) Marimer LLC