Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq to SQL逻辑错误,比较2个列表时不返回值_C#_Linq_Visual Studio - Fatal编程技术网

C# Linq to SQL逻辑错误,比较2个列表时不返回值

C# Linq to SQL逻辑错误,比较2个列表时不返回值,c#,linq,visual-studio,C#,Linq,Visual Studio,我试图比较两个列表,并通过比较这些列表中的3个字段来返回不匹配的项。比如说 List<Chemical> mList = mresult.ToList(); -- less 5 items List<Chemical> xList = xresult.ToList(); -- 7 items -- need 2 items back foreach (var List in xList ) { if (!(mList .Exists(x => x.MSD

我试图比较两个列表,并通过比较这些列表中的3个字段来返回不匹配的项。比如说

List<Chemical> mList = mresult.ToList();  -- less 5 items
List<Chemical> xList = xresult.ToList(); -- 7 items -- need 2 items back

foreach (var List in xList )
{
    if (!(mList .Exists(x => x.MSD_ID == List.MSD_ID)) && !(mList .Exists(y => y.Roman_ID == List.Roman_ID)) && !(mList .Exists(z => z.Source_System_ID == List.Source_System_ID)))
    {
        Chemical x = new Chemical
        {
            MSD_ID = List.MSD_ID,
            Roman_ID = List.Roman_ID,
            Source_System_ID = List.Source_System_ID
        };
        unmatchedList.Add(x);  --need 2 items but returns none
    }             
} 
List mList=mresult.ToList();--减5项
List xList=xresult.ToList();--7件物品--需要2件物品
foreach(xList中的var列表)
{
如果(!(mList.Exists(x=>x.MSD_ID==List.MSD_ID))&&(mList.Exists(y=>y.Roman_ID==List.Roman_ID))&&(mList.Exists(z=>z.Source_System_ID==List.Source_System_ID)))
{
化学品x=新化学品
{
MSD_ID=List.MSD_ID,
Roman_ID=List.Roman_ID,
Source\u System\u ID=List.Source\u System\u ID
};
unmatchedList.Add(x);--需要2项,但不返回任何项
}             
} 
我真的想知道我在哪里错过了它。提前感谢所有人

包括NuGet提供的软件包并使用:

List<Chemical> mList = mresult.ToList();  -- less 5 items
List<Chemical> xList = xresult.ToList(); -- 7 items -- need 2 items back
var unmatchedList = xList.ExceptBy(mLlist,x=>new {x.MSG_ID,x.Roman_ID,x.Source_System_ID});
List mList=mresult.ToList();--减5项
List xList=xresult.ToList();--7件物品--需要2件物品
var unmatchedList=xList.ExceptBy(mLlist,x=>new{x.MSG_ID,x.Roman_ID,x.Source_System_ID});
或者,您可以实现IEqualityComparer,如下所示:

如果您的化学类只有这3个字段,则只需:

List<Chemical> mList = mresult.ToList();  -- less 5 items
List<Chemical> xList = xresult.ToList(); -- 7 items -- need 2 items back
var unmatchedList = xList.Except(mLlist);
List mList=mresult.ToList();--减5项
List xList=xresult.ToList();--7件物品--需要2件物品
var unmatchedList=xList.Except(mLlist);

是否检查了if语句的所有条件是否正确?如果它们是真的,那么它肯定不会返回任何东西@RamSo你有三个谓词-我会分别执行它们,并将结果存储在局部变量中,这样在调试时更容易看到发生了什么。此外,你为什么声明
xList
而从不使用它?@JonSkeet-我编辑了这个问题。我会照你说的做,看看。。。。。我实际上是将xList的每个项目与所有mList项目进行比较,看看是否有不匹配的地方。问题在于逻辑(我相信)。您正在检查每个属性是否存在,而不是检查所有3个属性是否匹配的记录。我相信您正在寻找的if语句是
if(!(mList.Exists(x=>x.MSD\u ID==List.MSD\u ID&&x.Roman\u ID==List.Roman\u ID&&x.Source\u System\u ID==List.Source\u System\u ID))
,但我相信下面给出的答案更好。如果您提供示例输入和预期输出,您会得到更好的答案。您的意思是除了而不是ExceptBy吗?不,ExceptBy是MoreLinq中的一个函数。它类似于Except,但您可以提供要比较的字段,而不是为Except编写IEqualityComparer。默认情况下,Exception会比较所有字段。如果Chemical只有这3个字段,那么Exception会很好地工作,但是由于您没有包含它的定义,我假设它不止这3个字段,但这可能是一个不好的假设。更新了答案,以防化学只有3个字段。当我使用“除外”时,它会返回较大列表中的所有值。因为它与Source\u System\u ID比较失败,并且总是返回false。这是一个字符串字段,在我看来它们是一样的-(我会用我们的发现来改变问题,这样你就能理解我的意思。谢谢你的帮助,罗伯特。需要样本输入。它不应该这样做,所以我不明白问题是什么。