gpt4 book ai didi

c# - 用于检查重复值的 Linq 查询

转载 作者:太空狗 更新时间:2023-10-29 20:21:16 26 4
gpt4 key购买 nike

在我的一个页面中,我需要检查输入的有关客户的信息是否包含重复的 PAN NO,Email,Mobile No,这可能是之前输入的。目前我正在尝试使用此 Linq To SQL 语句

    var duplicate = (from dup in dt.Data_Customer_Logs
where dup.cPanGirNo == panno
|| dup.cEmail == email
|| dup.nMobileNo.ToString() == mobno
select dup).Any();

它正在工作,但任何人都可以帮助我解决问题的正确方法是什么。另外,如果没有找到记录,结果会是什么。欢迎提出任何建议。

最佳答案

bool duplicateExists = dt.Data_Customer_Logs.Any(x => 
x.cPanGirNo == panno
|| x.cEmail == email
|| x.nMobileNo.ToString() == mobno);

如果您只想知道此类记录是否存在,这会更简洁一些。而且我认为这将避免将多条记录带回客户端然后执行IEnumerable<T>.Any关于结果。

如果你还需要取回符合条件的记录,你可以使用IQueryable<T>.Where :

var duplicates =  dt.Data_Customer_Logs.Where(x => 
x.cPanGirNo == panno
|| x.cEmail == email
|| x.nMobileNo.ToString() == mobno);
if(duplicates.Any())
{
// use duplicates...
foreach(var dup in duplicates)
{
//use dup.cEmail, dup.nMobileNo, etc.

关于c# - 用于检查重复值的 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11096241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com