简体   繁体   English

如何在C#中的linq的选择中放置条件

[英]How to put a condition in the select of linq in C#

I want to select values from LINQ我想从 LINQ 中选择值

but these values depend on another control in my windows form in C#但是这些值取决于我在 C# 中的 Windows 窗体中的另一个控件

Here is my code这是我的代码

var lstInfo = grdBreakDown.Rows.Cast<DataGridViewRow>()
    .Where(x => !x.IsNewRow)
    .Select(x => (tag: x.Cells["tag"].Value.ToString(), Scheme: x.Cells["Scheme"].Value.ToString(), Value: x.Cells["Value"].Value.ToString()))
    .Distinct()
    .ToList();

lvwInfo.Items.AddRange( lstInfo
    .Select(x => new ListViewItem(new string[] { x.tag, x.Scheme, x.Value }))
    .ToArray()
);

This code works perfectly and displays all Schemes something like this此代码完美运行并显示所有类似这样的方案

    Tag  Scheme  Value
    Red  Master  88
    Red  Master  14
    Red  Admin   39
    Red  Admin   31
    Blue Master  91
    Blue Master  10
    Blue Admin   54
    Blue Admin   04

But I want to make a few changes and only displays Schemes & Value when a checkbox in the windows form is ticked and if not ticked just display empty string但是我想做一些更改,并且仅在 Windows 窗体中的复选框被勾选时才显示 Schemes & Value,如果未勾选则只显示空字符串

something like this像这样的东西

    .Select(x => (
        tag: x.Cells["tag"].Value.ToString(),
        Scheme:
            if(
                (!cbxMaster.checked &&  x.Cells["Scheme"].Value.ToString() == "Master")
                ||
                (!cbxAdmin.checked &&  x.Cells["Scheme"].Value.ToString() == "Admin"), "", x.Cells["Scheme"].Value.ToString()
            ),
        Value:
            if((!cbxMaster.checked &&  x.Cells["Scheme"].Value.ToString() == "Master")
                ||
                (!cbxAdmin.checked &&  x.Cells["Scheme"].Value.ToString() == "Admin"), "", x.Cells["Value"].Value.ToString()
            )
    )

So if Master ticked an Admin is not ticked因此,如果 Master 打勾,则 Admin 未打勾

output will be like this输出将是这样的

    Tag  Scheme  Value
    Red  Master  88
    Red  Master  14
    Red     
    Blue Master  91
    Blue Master  10
    Blue 

but this is not working.但这不起作用。

how to do that with linq?如何用 linq 做到这一点?

To modify what you have selected, you have to browse through the entire selection.要修改您选择的内容,您必须浏览整个选择。

var lstInfo = grdBreakDown.Rows.Cast<DataGridViewRow>()
                               .Where(x => !x.IsNewRow)
                               .Select(x => (tag: x.Cells["tag"].Value.ToString(), Scheme: x.Cells["Scheme"].Value.ToString(), Value: x.Cells["Value"].Value.ToString()))
                               .Distinct()
                               .ToList()
                               .foreach (elem=>{
                                     if(elem.Sheme.equals("Admin")){
                                         elem.Sheme="";
                                         elem.value="";}
                                });

Why not try a conditional operator for the value you are searching for?为什么不为您正在搜索的值尝试条件运算符?

.Where(x => x.Cells["Scheme"].Value.ToString() == ((checked) ? "Master" : "Admin") )

Checked could be a bollean value or whether the tickbox is ticked. Checked 可能是一个 bollean 值或是否勾选了复选框。 I think this will work我认为这会奏效

Define the problem定义问题

You could translate that to: Get the values of the Admin and/or Master rows if the corresponding CheckBox controls are checked.您可以将其转换为: 如果选中相应的CheckBox控件,则获取Admin和/或Master行的值。 Otherwise, get their distinct tags.否则,获取它们不同的标签。

Thus:因此:

private void PopulateListView()
{
    // Local function...
    (string tag, string scheme, string value) GetItems(DataGridViewRow row) =>
        (chkAdmin.Checked && row.Cells["scheme"].Value.ToString() == "Admin") ||
        (chkMaster.Checked && row.Cells["scheme"].Value.ToString() == "Master")
        ? (
        row.Cells["tag"].Value.ToString(),
        row.Cells["scheme"].Value.ToString(),
        row.Cells["value"].Value.ToString())
        : (
        row.Cells["tag"].Value.ToString(),
        string.Empty,
        string.Empty);

    var lstInfo = dgv.Rows.Cast<DataGridViewRow>()
        .Where(x => !x.IsNewRow)
        .Select(x => GetItems(x))
        .Distinct()
        .Select(x => new[] { x.tag, x.scheme, x.value });

    lvwInfo.Items.Clear();
    lvwInfo.Items.AddRange(lstInfo.Select(x => new ListViewItem(x)).ToArray());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM