0

エラーが発生しました

cannot implicity convert type 'System.Linq.IQueryable<EntityNetimoveis.San_Imovel>' to 'EntityNetimoveis.San_Imovel' An Explicit conversion exists (are you missing a cast ?)

これは、次のコードを試すと発生します

EntityNetimoveis.San2011Entities db = new EntityNetimoveis.San2011Entities();
EntityNetimoveis.San_Imovel im = db.San_Imovel.Where(a => a.Credenciada_Id == 10);

どのタイプの変換を行う必要がありますか?

4

2 に答える 2

3
EntityNetimoveis.San_Imovel im = db.San_Imovel.Where(a => a.Credenciada_Id == 10);

IQueryable<EntityNetimoveis.San_Imovel>where クエリを実行していて、複数の結果が存在する可能性があるため、im のタイプは anです。クエリに一致する最初の結果が必要な場合は、.FirstOrDefault() を使用する必要があります。コードを次のように変更してみてください。

EntityNetimoveis.San_Imovel im = db.San_Imovel.FirstOrDefault(a => a.Credenciada_Id == 10);

複数のエンティティを返して処理する場合、たとえば foreach ループを使用する場合は、戻り値の型を に変更する必要がありますIEnumerable<Netimoveis.San_Imovel>。次の例を参照してください。

IEnumerable<EntityNetimoveis.San_Imovel> ims = db.San_Imovel.Where(a => a.Credenciada_Id == 10);

次のものを使用できます。

foreach(var im in ims) {
    // your code goes here
}
于 2012-08-24T13:54:02.097 に答える
2

ラムダが複数のレコードを返すことができる場合:

var imList = db.San_Imovel.Where(a => a.Credenciada_Id == 10).ToList();
于 2012-08-24T13:53:45.483 に答える