检查隐式类型变量是否包含特定值

发布于 2024-11-26 23:42:42 字数 900 浏览 0 评论 0原文

我有一个隐式变量,它采用一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞳孔里扚悲伤 2024-12-03 23:42:42

匿名类型通过检查每个属性来测试相等性,因此您可以这样做:(

if (!existingClients.Contains(new { record.CONTACTID, record.FULLNAME }))

假设您的 record 变量具有 FULLNAME 属性。否则只需使用适当的值构造匿名对象。)

请参阅此答案了解更多信息。

编辑

更简单的方法可能是:

if (!existingClients.Any(c => c.CONTACTID == result.CONTACTID))

Anonymous types test for equality by checking each property, so you could do this:

if (!existingClients.Contains(new { record.CONTACTID, record.FULLNAME }))

(Assuming your record variable has a FULLNAME property. Otherwise just construct the anonymous object with the appropriate values.)

See this answer for more info.

Edit

Easier still might be:

if (!existingClients.Any(c => c.CONTACTID == result.CONTACTID))
━╋う一瞬間旳綻放 2024-12-03 23:42:42

感谢马特的好回答!就性能而言,您的建议与使用临时列表来存储 ID 并检查该列表之间有什么区别?

...
List<Guid> existIDs = new List<Guid>();
foreach (var c in existingContacts)
{
  existIDs.Add(c.CONTACTID);
}

var resultSet = db.sp_PagingActiveContacts(idx, userLogin, range).ToList();
foreach (var record in resultSet)
{
if (!existIDs.Contains(new {record.CONTACTID}))
{  ....

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?

...
List<Guid> existIDs = new List<Guid>();
foreach (var c in existingContacts)
{
  existIDs.Add(c.CONTACTID);
}

var resultSet = db.sp_PagingActiveContacts(idx, userLogin, range).ToList();
foreach (var record in resultSet)
{
if (!existIDs.Contains(new {record.CONTACTID}))
{  ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文