Linq-to-SQL 查询列表

Linq-to-SQL query a list

这很可能非常简单,只是我无法使其正常工作。

我有 2 个 c# 类:

市场

public string MarketId { get; set; }

public string Description { get; set; }

public virtual List<Dealer> Dealers { get; set; }

经销商

public string DealerId { get; set; }

public string City { get; set; }

public Market Market { get; set; }

我想做的是获取一个或多个特定市场 (marketid),并且只获取与 (dealerid) 匹配的经销商。可以是多个匹配项。

我试过这个:

dbMarkets = db.Markets.Where(x => x.MarketId.Equals(marketId) && x.Dealers.Select(c => c.DealerId).Contains(dealerId)).ToList();

然而 returns 属于市场的所有经销商,而不仅仅是匹配 dealerid 的经销商。

行中的内容:

db.Markets
  .Where(o => o.MarketId.Equals(marketId))
  .Select(o => new Market{ 
    MarketId = o.MarketId,
    Description = o.Description,
    Dealers = db.Dealers.Where(k => k.DealerId == o.Dealers).ToList()
  })
  .ToList();

由于Market.Dealers属性只是一个导航,需要单独加载经销商。但是在这种情况下,每个Dealer也有Market作为导航属性,您可以先查询到Dealer包括Market,然后按[=15分组=] 之后。像这样:

using (var db = new MyContext())
{
    // this is important since Market.Dealers is a navigation property,
    // without this it would load all dealers in the market.
    db.Configuration.LazyLoadingEnabled = false;

    // load all dealers and its market(using Include)
    // by specific marketId and dealerId.
    var dealers = db.Dealers
        .Include(o => o.Market)
        .Where(o => o.DealerId == dealerId && o.Market.Id == marketId)
        .ToList();

    // this will be executed in the client-side, 
    // so you don't need to worry about round-trip overhead.
    var markets = dealers
        .GroupBy(o => o.Market)
        .Select(o => o.Key)
        .ToList();
    Console.WriteLine("Check if all dealers also have market: " +
                  markets.SelectMany(o => o.Dealers).All(o => o.Market != null));
    Console.WriteLine("All dealers in the markets: " +
                  markets.SelectMany(o => o.Dealers).Count());
}

如果您不喜欢设置配置或编写额外的代码(在本例中为分组),您可以将市场信息和经销商投射为匿名对象。但是这种方法无法在每个经销商上保留对市场 属性 的引用。

using (var db = new MyContext())
{
    var anonymousMarkets = db.Markets
        .Where(o => o.MarketId == marketId)
        .Select(o => new        // project as an anonymous object
        {
            o.MarketId,
            o.Description,
            Dealers = o.Dealers.Where(d => d.DealerId == dealerId)
        })
        .ToList();
    Console.WriteLine("Check if all dealers don't have market: " +
        anonymousMarkets.SelectMany(o => o.Dealers).All(o => o.Market == null));
    Console.WriteLine("All dealers in the markets: " + 
        anonymousMarkets.SelectMany(o => o.Dealers).Count());
}

最后,如果您想在每个经销商上保留市场导航 属性,您可以将市场和经销商聚合在一起。

using (var db = new MyContext())
{
    var marketAggregates = db.Markets.Where(o => o.MarketId == marketId)
        .Select(o => new    // aggregate Market and Dealers as an anonymous object.
        {
            Market = o,
            Dealers = o.Dealers.Where(d => d.DealerId == dealerId)
        })
        .ToList();
    Console.WriteLine("Check if all dealers also have market: " +
          marketAggregates.SelectMany(o => o.Dealers).All(o => o.Market != null));
    Console.WriteLine("All dealers in the markets: " + 
          marketAggregates.SelectMany(o => o.Dealers).Count());
}

好吧,所有的方法都有不同的优缺点,但我通常更喜欢最后一种。