从结果等于List <t>中的多个值的实体中进行选择

时间:2016-10-14 02:24:15

标签: asp.net linq entity

说美国城市有一个对象

public class City
{
    public int Id
    public String State
    public String City
}

我有一个城市和州的列表

List<Cities> Cities = new List<Cities>();
Cities.Add(new City { City = "Philadelphia", State = "PA" });
Cities.Add(new City { City = "New York", State = "NY" });

我想将我的sql数据库设置搜索为

Id int
City varchar
State varchar

获取Full对象,主要是Ids 我尝试了很多方法作为我的最新方法

var results = (from Cities in c.Cities
    where distinctValues.Select(m => 
    m.City).ToList().Contains(Cities.City)                               
    && distinctValues.Select(m => 
    m.State).ToList().Contains(Cities.State)
                           select new City
                           {
                               City = Cities.City,
                               State= Cities.State,
                               Id = Cities.Id
                           }).ToList();

给我错误

Unable to create a constant value of type.

有没有办法搜索记录匹配List对象中多个属性的位置

我尝试做我在创建sql查询时所做的事情,该查询迭代列表中的每个项目并为每个项目添加参数,例如

select Id,City,State FROM Cities WHERE (city = @city1 and state = @state1) or (city = @city2 and state = @state2)

其中

@city1 = philadelphia,@state1 = PA, @city2 = new york and @state2 = ny

0 个答案:

没有答案