我想在实体框架中从地址表返回4列。但是它在" address = addressData.toString();"说不能隐式地将类型字符串转换为System.Collection.Generic.List
[WebMethod]
public List<string> getAddressSearchSimple(string addressInput)
{
List<string> address = new List<string>();
try
{
using (GISAddressEntities database = new GISAddressEntities())
{
var addressData = (from table in database.view_COBADDRESS
where table.ADD_FULL.Contains(addressInput)
select new { table.ADD_FULL, table.POSTALCITY, table.STATE, table.ZIP5});
address = addressData.ToString();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return address;
}
答案 0 :(得分:0)
address是一个字符串列表,因此您不能将任何对象设置为ToString()。
如果addressData应该是一个字符串列表来尝试这个:
var addressData = (from table in database.view_COBADDRESS
where table.ADD_FULL.Contains(addressInput)
select new { table.ADD_FULL, table.POSTALCITY,
table.STATE, table.ZIP5}).ToList();
答案 1 :(得分:0)
从您的问题来看,我认为这可能是您正在寻找的:
address = addressData.ToList().Select(addr => string.Format("{0} {1} {2} {3}", addr.ADD_FULL, addr.POSTALCITY,
addr.STATE, addr.ZIP5)).ToList();
答案 2 :(得分:0)
您应该调用ToList()方法而不是ToString()来创建List。您还应该从查询中选择字符串以获得List<string>
。试试这个:
public List<string> getAddressSearchSimple(string addressInput)
{
List<string> address = new List<string>();
try
{
using (GISAddressEntities database = new GISAddressEntities())
{
var addressData = (from table in database.view_COBADDRESS
where table.ADD_FULL.Contains(addressInput)
select new { table.ADD_FULL, table.POSTALCITY, table.STATE, table.ZIP5 });
address = addressData.Select(x => string.Format("{0} {1} {2} {3}", x.ADD_FULL, x.POSTALCITY, x.STATE, x.ZIP5))
.ToList();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return address;
}
答案 3 :(得分:0)
您的问题是addressData
不是字符串列表 - 它是包含select
中4个字段的临时类的列表。如果您修改select
以仅获取一个字段,则会起作用。
但是,正如你所说,你想要这四个字段,我建议你创建一个名为maybe AddressDetails
的新类,每个字段都有一个属性。然后你可以简单地写:
AddressDetails data = (from table in database.view_COBADDRESS
where table.ADD_FULL.Contains(addressInput)
select new { table.ADD_FULL, table.POSTALCITY, table.STATE, table.ZIP5 });
return data;
更改了要匹配的方法的返回类型。