您好,我有 2 个数据表(bannedlist、countrylist),两者都包含国家/地区名称列表以及 cc 和国家列中的 cod。我正在尝试执行一个查询,我可以从国家列表表中选择不在禁止列表表中的国家/地区,以便创建第三个表。
有任何想法吗?
我对此还没有太了解。
var ccList = ds.Tables[2].AsEnumerable();
var bannedCCList = ds.Tables[1].AsEnumerable();
var query = from r in ccList....
..
尝试后
var bannedCCList = ds.Tables[1].AsEnumerable();
var query = from r in ccList where !bannedCCList.Any(b => b["cc"] == r["cc"])select r;
我仍然得到相同的国家/地区列表。被禁止的还没有被删除。这里有更多细节以便解释更多。不确定我做错了什么
protected void BindCountryBan(string subd)
{
DataSet ds = new DataSet();
ds = new DB().CountryBan_GetSiteSettings();
BannedCountryListBox.DataSource = ds.Tables[1];
BannedCountryListBox.DataValueField = "cc";
BannedCountryListBox.DataTextField = "country";
BannedCountryListBox.DataBind();
//bind country list
var ccList = ds.Tables[2].AsEnumerable();
var bannedCCList = ds.Tables[1].AsEnumerable();
var query = from r in ccList where !bannedCCList.Any(b => b["cc"] == r["cc"])select r;
//var query = ccList.Except(bannedCCList);
//CountryListBox.DataSource = ds.Tables[2];
DataTable boundTable = query.CopyToDataTable<DataRow>();
CountryListBox.DataSource = boundTable;
CountryListBox.DataValueField = "cc";
CountryListBox.DataTextField = "country";
CountryListBox.DataBind();
}
如果您在国家/地区序列上使用它,则除外:
using System.Linq;
...
var ccList = from c in ds.Tables[2].AsEnumerable()
select c.Field<string>("Country");
var bannedCCList = from c in ds.Tables[1].AsEnumerable()
select c.Field<string>("Country");
var exceptBanned = ccList.Except(bannedCCList);
如果您需要未禁止的国家/地区的完整行,您可以尝试左外连接:
var ccList = ds.Tables[2].AsEnumerable();
var bannedCCList = ds.Tables[1].AsEnumerable();
var exceptBanned = from c in ccList
join b in bannedCCList
on c.Field<string>("Country") equals b.Field<string>("Country") into j
from x in j.DefaultIfEmpty()
where x == null
select c;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)