检查隐式类型变量是否包含特定值
我有一个隐式变量,它采用一个包含 SQL 查询 (LINQ) 结果的列表。 该集合代表先前选择的列表中的现有客户端。
通过存储过程,我获取了一组新的客户端,这些客户端将用于填充下拉列表。迭代这组数据时,如果这些记录已包含在现有条目列表中,我会排除这些记录。
我如何使用隐式类型变量进行此检查?
var existingClients = (from doc in TBL_Documents
join c in TBL_CONTACT on doc.CONTACTID equals c.CONTACTID
where doc.DOCID.Equals(DocID)
select new { c.CONTACTID, c.FULLNAME }).ToList();
var resultSet = sp_ActiveContacts(index).ToList();
foreach (var record in resultSet)
{
//How could I achieve something like this if condition (like a List)?
if (!existingClients.Contains(record.CONTACTID))
{
optionTags += "<option value=" + record.CONTACTID + ">" + record.Name + "</option>";
}
}
如果可能的话,我宁愿避免创建一个临时列表来存储用于 if 条件的 CONTACTID。
I have an implicit variable taking a List containing the result of a SQL query (LINQ).
This set represents the existing clients of a list, previously selected.
Through a stored procedure I take a new set of clients that will be used to fill a drop down. Iterating through this set of data I exclude those records if already contained in the list of existing entries.
How could I do this check with implicitly typed variables?
var existingClients = (from doc in TBL_Documents
join c in TBL_CONTACT on doc.CONTACTID equals c.CONTACTID
where doc.DOCID.Equals(DocID)
select new { c.CONTACTID, c.FULLNAME }).ToList();
var resultSet = sp_ActiveContacts(index).ToList();
foreach (var record in resultSet)
{
//How could I achieve something like this if condition (like a List)?
if (!existingClients.Contains(record.CONTACTID))
{
optionTags += "<option value=" + record.CONTACTID + ">" + record.Name + "</option>";
}
}
If possible, I would prefere avoiding to create a temporary List to store the CONTACTID used for the if condition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
匿名类型通过检查每个属性来测试相等性,因此您可以这样做:(
假设您的
record
变量具有FULLNAME
属性。否则只需使用适当的值构造匿名对象。)请参阅此答案了解更多信息。
编辑
更简单的方法可能是:
Anonymous types test for equality by checking each property, so you could do this:
(Assuming your
record
variable has aFULLNAME
property. Otherwise just construct the anonymous object with the appropriate values.)See this answer for more info.
Edit
Easier still might be:
感谢马特的好回答!就性能而言,您的建议与使用临时列表来存储 ID 并检查该列表之间有什么区别?
Thanks Matt for the good answer! In terms of performance what would be the difference between your suggestion and using a temporary List to store the IDs and make the check over this list?