LINQ: Calling a Method from within linq?, my method returns an anonymous type
Can anyone help. I have the following as part of a LINQ, its working but the object MyObject returns an anonymous type. (I checked it in debug mode)
{System.Linq.Enumerable.WhereSelectEnumerableIterator<<>f__AnonymousType1<MyType.ParamRow,MyType.Row>,object>}
Here is part of my query
select new Param
{
Name ="Test1",
MyObject = (from o1 in otherO开发者_StackOverflowbject.Object join o2 in otherObject.ObjectB on
o1.Id equals o2.Id
where o.Id == o2.Id select this.BuildMyObject("DataObject", o1))
}
I think i am using the select wrong....
Basically MyObject returns type of object but this
select this.BuildMyObject("DataObject", o1)
returns an anonymous type. I want to create a real type of Object..
I think I am doing something wrong.
EDIT
Build my object is very simple currently I am returning a TextBox (textbox is a class of mine) as you can see it returns OBJECT
private object BuildMyObject(string type, TestStructure.ParamRow item)
{
MyTextBox control = new MyTextBox();
control.Name = "NotEmpty";
return control
}
That is how the compiler implements joins; it creates an anonymous type representing the various forms. You should be able to fix this in a select, for example:
select Tuple.Create(o1, o2);
or if you only need o1
:
select o1;
Looking at your MyObject, I believe all you are seeing here is the concrete type that is underpinning an IEnumerable<object>
. This is because you aren't limiting to a single object. If you expect one row, tell it:
select new Param
{
Name ="Test1",
MyObject = (from o1 in otherObject.Object
join o2 in otherObject.ObjectB on o1.Id equals o2.Id
where o.Id == o2.Id select this.BuildMyObject("DataObject", o1)
).First()
}
Otherwise, just access MyObject
as an IEnumerable<object>
, and you should find that each row is actually a MyTextBox
; i.e.
var sequence = (IEnumerable<object>) param.MyObject;
foreach(MyTextBox tb in sequence) {
// should work
}
but note that this is different to it being an IEnumerable<MyTetxBox>
; for that, you would use .Cast<MyTextBox>()
精彩评论