c# - .Except LINQ 在 MVC 中构建时不会排除值,而是在简单的控制台应用程序中排除值。为什么?
问题描述
我试图在问题标题中详细说明。在 MVC 中构建应用程序。我需要一种方法来构建一个数组减去发送到控制器的值。我可以在一个简单的控制台应用程序中轻松地做到这一点,但是 .Except 方法在控制器中使用时似乎功能不同。
我有一个小方法的静态数组。我向该方法发送了一些值并使用 except 方法创建一个新数组。完全按照应有的方式工作。
static void Main(string[] args)
{
string[] sublines = new string[] { "BI/PD", "Hired", "PIP", "Medical Payments" };
results(sublines);
}
public static void results(string[] values)
{
string[] subLineVales = new string[] { "BI/PD", "Hired", "Non-Owned", "PIP", "Addtl-PIP", "Medical Payments", "UM PD", "UM CSL", "UIM CSL", "Terrorism" };
int count = subLineVales.Length;
string[] DifferArray = subLineVales.Except(values).ToArray();
foreach (var item in DifferArray)
{
Console.Write(item);
Console.ReadLine();
}
新数组不包括“values”数组参数中的值。
其次,我采用相同的方法并将数组中的特定值发送到 MVC 控制器。
public ActionResult MyAction(string[] rid, string[] subLineNames, string[] lineGuids, string uniqline, string uniqpolicy, string uniqlinetype)
{
string[] sublineArray = new string[] { "BI/PD", "Hired", "Non Owned", "PIP", "Addtl PIP", "Medical Payments", "UM PD", "UM CSL", "UIM CSL", "Terrorism" };
string[] subLineValues = sublineArray.Except(subLineNames).ToArray(); //Fills array with sublineArray. Doesn't exclude any of the values from the subLineNames array. And Yes, I've checked to ensure string[] subLineNames actually has values.
这应该与控制台应用程序以相同的方式工作,但正如我上面所说,它只是没有。它只采用 sublineArray 值并将它们全部插入,而它应该只采用那些不在 subLineNames 数组中的值。几天来,我一直在努力寻找解决此问题的不同方法。Haalpp ..这让我发疯了。
编辑。在 subLineNames 参数上添加一些说明。
//Listen for the MCPD submit button
$('#mcpdSubmit').on('click', function () {
getSublineValues();
});
function getSublineValues() {
var rid = []; //store RID's
var subLineNames = []; //store subLine names
var Guids = []; //store subLine Guids
//find selected checkboxes and push values
$("table.subLineTable tr").each(function () {
if ($(this).find('td input[type="checkbox"]').is(':checked'))
rid.push($(this).find('td .mcalRIDS').html());
if ($(this).find('td input[type="checkbox"]').is(':checked'))
subLineNames.push($(this).find('td .subLineMCAL').html());
if ($(this).find('td input[type="checkbox"]').is(':checked'))
Guids.push($(this).find('td .mcalGuids').html());
});
var ul = $.trim($('#mcUl').text()); //MCAL UniqLine variable
var up = $.trim($('#mcUp').text());
var MCAL = '5cc3cb18-5b52-454d-88c7-2670501946b4';
var div = $('#MCAL').html();
var sendData = JSON.stringify({'rid': rid, 'subLineNames': subLineNames, 'lineGuids': Guids, 'uniqline': ul, 'uniqpolicy': up, 'uniqlinetype': MCAL });
$.ajax({
url: '/Trucking/MyAction',
type: 'POST',
contentType: 'application/json',
data: sendData,
success: function () {
//alert('success');
console.log('success');
$('#MCAL').html(div);
},
error: function () {
//alert('Error');
console.log('Error');
}
});
// location.reload();
alert(subLineNames); //I did this to ensure I was getting the correct values. When submitted, all the values come up correctly.
解决方案
听起来您的字符串返回了额外的空格。在方法端处理此问题的一种方法是调用Trim
传入列表中的每个项目。此外,根据数据源,使用不区分大小写的比较可能是个好主意:
string[] diffArray = subLineVales
.Except(values.Select(value => value.Trim()), StringComparer.OrdinalIgnoreCase)
.ToArray();
推荐阅读
- node.js - 是的验证将空字符串转换为默认值
- python - 尝试解决凸最小化/二进制编程问题时出现 CVXPY 错误
- java - 在 Vaadin14 中的 Grid、TreeGrid 上使用 Selection.MultiSelect 时的空白选择列
- angular - 如何点击 TypeScript/Angular 中的 observable 返回?
- azure - 来自流分析的 Azure Function HTTPTrigger
- syntax-highlighting - 为什么 CLion 不以不同的方式突出 Doxygen 评论?
- c# - 当多个用户同时上传到数据库时,AjaxFileUpload 临时数据保存到错误的帐户
- python - 无法使用 Python 绘制包含二进制值的列的分布
- c# - 如何在 c# 中拍摄没有 vsync 的 directX11 游戏的良好屏幕截图
- rest - 如何通知客户端返回的 HTTP 资源表示已弃用?