【问题标题】:LINQ query limits returned values to those matched in List<String>LINQ 查询将返回值限制为 List<String> 中匹配的值
【发布时间】:2014-03-23 11:51:58
【问题描述】:
var test = new List<String>();
test.Add("Wendys");
test.Add("Olive Garden");
test.Add("McDonalds");
test.Add("Fridays");

(from r in db.Restaurants where r.Restaurant_Name.StartsWith(restName) 
&& r.RestaurntName.Contains(List<String> test) 
select r.Restaurant_Name).Take(matchingCount).toList();    

如何获取正确的代码,使其仅从 Restaurant DB 返回与 List&lt;string&gt; 中的项目匹配的餐厅名称

【问题讨论】:

    标签: c# asp.net linq-to-sql contains


    【解决方案1】:

    你应该反转表达式的顺序

    (from r in db.Restaurants where r.Restaurant_Name.StartsWith(restName) 
    && r.any(c=>test.contains(r.Restaurant_Name))
    select r.Restaurant_Name).Take(matchingCount).toList();    
    

    【讨论】:

      【解决方案2】:

      你的意思是这样的吗?

      db.Restaurants
        .Where(x => txt.Contains(x.Restaurant_Name))
        .Select(x => x.Restaurant_Name)
        .Take(matchingCount).ToList();
      

      【讨论】:

        【解决方案3】:

        试试这个:

        list<string> myOptions = db.Restaurants.Where(r=> 
                r.Restaurant_Name.StartsWith(restName) && 
                test.Contains(r.Restaurant_Name))
           .Select(r => r.Restaurant_Name).take(matchingCount).toList();
        

        这应该返回一个字符串对象列表

        我不确定您想要的是否只是 1 个选项,如果是,请尝试以下操作:

        string myOption = db.Restaurants.FirstorDefault(r=> 
                r.Restaurant_Name.StartsWith(restName) && 
                test.Contains(r.Restaurant_Name))
           .Select(r => r.Restaurant_Name);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-20
          • 1970-01-01
          • 1970-01-01
          • 2011-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多