如何对LINQ执行SQL的“IN”和“DISTINCT”

时间:2017-08-04 14:07:12

标签: c# sql linq

SELECT DISTINCT MarketTemplateId AS MarketTemplateID
FROM Market
WHERE Market.FixtureId = ? And
    Market.MarketTemplateId In 
    ('MW3W', '1HTG', 'FTCS', 'HTFT', 'MTG2W', 'MW3W1H','FTCSALT')

我将如何在LINQ中生成这个,因为我尝试了以下代码并且它不起作用

var result6 = entityTrading.Markets
            .Where(p => p.FixtureId == InternalFixtureID_F.ToString())
            .FirstOrDefault(p => MarketTemplateIds.Contains(p.MarketTemplateId));

2 个答案:

答案 0 :(得分:2)

List<string> MarketTemplateIds = new List<string>{ "MW3W", "1HTG", "FTCS", "HTFT", "MTG2W", "MW3W1H","FTCSALT" };

var result6 = entityTrading.Markets
            .Where(p => p.FixtureId == InternalFixtureID_F.ToString()
             and MarketTemplateIds.Contains(p.MarketTemplateId ))
            .Select( u => u.MaketTemplateId).Distinct();

答案 1 :(得分:1)

怎么样

var id = InternalFixtureID_F.ToString();

var result6 = entityTrading.Markets
              .Where(p => p.FixtureId == id  && MarketTemplateIds.Contains(p.MarketTemplateId))
              .Select(m => m.MarketTemplateId)
              .Distinct();