我如何能遍历一个C#的IEnumerable在Matlab?遍历、IEnumerable、Matlab

2023-09-05 01:34:53 作者:潮父

我有一些LINQ到SQL code在C#中,我试着用Matlab 2010年b访问。如果我的LINQ code返回单个项目例如,我可以没有问题访问Matlab的所有属性:

I have some Linq to SQL code in C# that I'm trying to access using Matlab 2010b. If my Linq code returns a single item instance, I can access all the properties without problem in Matlab:

dal = Data.PeopleRepository
person = dal.QueryPersonById(1)
person.Name

ans = 

John Smith

但是,如果我叫一个LINQ查询,返回一个IQueryable集合(实际上是System.Data.Linq.Table类),我挣扎着爬在人的实例里面!列表

But if I call a Linq query that returns an IQueryable collection (actually a System.Data.Linq.Table class), I'm struggling to get at the list of Person instances inside!

people = dal.QueryAllPeople()

people = 

System.Data.Linq.Table<Data.Person> handle
Package: System.Data.Linq
Properties:
   Context: [1x1 Data.PeopleRepository]
   IsReadOnly: 0

我试着使用GetEnumerator方法的人转换为一个IEnumerable,但我还是不能让在里面的真人实例。 (我知道,由于后期的评估值可能没有实际从数据库中检索过Aarggh!)任何指针AP preciated,并道歉,如果这是不明确 - 这是我的第一个计算器问题...

I've tried converting to an IEnumerable using the GetEnumerator method on people, but I still can't get at the actual Person instances inside. (I'm aware that due to late evaluation the values may not yet actually be retrieved from the database too! Aarggh!) Any pointers appreciated, and apologies if this is unclear - it's my first StackOverflow question...

推荐答案

我只工作Matlab和C#的其他方式(从C#调用Matlab的),但它看起来对我来说,问题是你有一个IQueryable实例而不是在你的code一个IEnumerable

I've only worked Matlab and C# the other way around (calling Matlab from C#) but it looks to me like the problem is that you have an IQueryable instance instead of an IEnumerable in your code.

尝试调用人= dal.QueryAllPeople()了ToList()人= dal.QueryAllPeople.ToArray() - 这将导致执行查询 - 然后将数据有望提供内部Matlab的。

Try calling people = dal.QueryAllPeople().ToList() or people = dal.QueryAllPeople.ToArray() - these will cause a query to be executed - and the data will then hopefully be available inside Matlab.

更新 - 因为扩展方法是一个没有没有......发生什么事,如果你尝试:

Update - since extension methods are a no-no... what happens if you try:

myEnumerator = dal.QueryAllPeople().GetEnumerator()
myEnumerator.MoveNext()
firstItem = myEnumerator.Current
firstItem =

myEnumerator.MoveNext()
secondItem = myEnumerator.Current
secondItem =