1

私はたくさん試しましたが、インターネット上で解決策を見つけることができません。コレクション内のプロパティの値を取得しようとしています。

これは DeliveryShop エンティティです:

[Key]
public int DeliveryShopId { get; set; }
public int MinOrderForDelivery { get; set; }
public bool HasDelivery { get; set; }
public bool HasTakeAway { get; set; }

public virtual List<Location> Locations { get; set; }

これは場所エンティティです:

[Key]
public int LocationId { get; set; }
public int DeliveryShopId { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string ZIP { get; set; }
public virtual DeliveryShop DeliveryShop { get; set; }

これは、インデックス アクション メソッドのクエリです。

viewModel.DeliveryShop = db.DeliveryShops.Where(c => c.Locations.Where(l => l.City == "Berlin")).ToList();

ベルリンにしかないショップを表示したいのですが、エラーが出ます。

4

1 に答える 1

0

あなたのLINQは意味がありません。おそらく次のようになります。

viewModel.DeliveryShop = db.DeliveryShops
  .Where(c => c.Locations
    .Any(l => l.City == "Berlin"))
  .ToList();

LINQ Where メソッドには( Func<TSource, bool> predicatebool を返すメソッド) が必要です。次のコードは bool を返しません。

c.Locations.Where(l => l.City == "Berlin")

コメントでPawelが言及しているように、これを行う別の方法は次のとおりです。

viewModel.DelipveryShop = db.Locations
  .Where(l => l.City == "Berlin") // returns IQueryable<Location>
  .Select(l => l.DeliveryShop)    // returns IQueryable<DeliveryShop>
  .ToList();  // returns List<DeliveryShop>
于 2013-10-10T18:47:14.023 に答える