var 型オブジェクトをクラスの配列にキャストします。
私の例では、テーブルからすべての要素をクエリできます。しかし、問題は私がそれをキャストするときです。すべてのインスタンスをキャストするわけではありません。何か助けて??
「var型」のような型はありません。を使用した宣言varは、コンパイラに変数の型を推測させるだけです。それはまだ静的に型付けされており、明示的に宣言したかのように機能します-匿名型を使用する変数を宣言することはできますが。
あなたの場合、関連するメソッドのいずれかが何をするのかわかりません。つまり、何が起こっているのかを実際に知ることはできません。QueryおそらくタイプのようですIEnumerable<AccessPointItem>。AccessPointItemからに変換する方法をコードで表現する必要がありますAccessPoint。
注意すべきいくつかのポイント:
tsvc.CreateQuery<AccessPointItem>()Select()nullを返すことはないので、チェックする必要はありませんCastキャストしようとします...それは本当にあなたが意図したものですか?AccessPointItemAccessPointThere is something wrong with the types involved.
First you read AccessPointItem:
var Query = from APs in tsvc.CreateQuery<AccessPointItem>(...)
Then you try to cast it to AccessPoint:
return Query.Cast<AccessPoint>()
You're gonna need to implement some kind of conversion method in order for that to work (unfortunately I never did it myself, but from a quick Google run I see plenty of info about the subject is available). I'll give it a shot off the top of my head:
//inside AccessPointItem.cs
public static AccessPoint ToAccessPoint(this AccessPointItem item) // extension method looks good for the job
{
AccessPoint retObj = new AccessPoint();
// assign properties from 'item' to 'retObj'
return retObj;
}
//Usage example in your case
return Query.Select(singleAccessPointItem => singleAccessPointItem.ToAccessPoint());
AccessPointクラスとを混同しているように見えますAccessPointItem。これを試して:
public static AccessPoint[] getAllAps()
{
return tsvc.CreateQuery<AccessPoint>("AccessPointTable").ToArray();
}
またはこれ:
public static AccessPointItem[] getAllAps()
{
return tsvc.CreateQuery<AccessPointItem>("AccessPointTable").ToArray();
}