Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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# 以后要进行过滤的建筑条件_C# - Fatal编程技术网

C# 以后要进行过滤的建筑条件

C# 以后要进行过滤的建筑条件,c#,C#,目标是使用一些代码为Where调用构建过滤条件 在FoxPro中,我将使用构建为字符串foo==3&&bar>5的条件,然后执行它 我正在C中尝试类似的方法-创建字符串,并在下面的代码中将其用作条件,但找不到方法: string Condition = ""; ... if (xyz > 0) Condition = "scr.ZipCode = 12345"; if (xyz > 1) Condition = "scr.ZipCode = 23456"; if

目标是使用一些代码为Where调用构建过滤条件

在FoxPro中,我将使用构建为字符串foo==3&&bar>5的条件,然后执行它

我正在C中尝试类似的方法-创建字符串,并在下面的代码中将其用作条件,但找不到方法:

string Condition = "";

...
if (xyz > 0)
    Condition = "scr.ZipCode = 12345";

if (xyz > 1)
    Condition = "scr.ZipCode = 23456";

if (xyz > 2)
    Condition = "scr.ZipCode = 34567";

...etc.
然后在代码中使用它:

var shippingShipCalculatorRecords = 
   _shippingShipCalculatorService.GetAllShippingShipCalculatorRecords()
            .Where(scr => (
                              (scr.CountryId == 0 && Condition)
                          )
            .OrderBy(x => x.Sequence).ToList();

我试着把它转换成bool,但也没用。我过去在FoxPro工作,可以很容易地实现它和条件。

对不起,但恐怕你不能这样做。但是,您可以在变量中保留邮政编码,并在以后的情况下使用它:

string zipcode = string.Empty; //Or an int, if it is stored like so

if (xyz > 0)
    zipcode = "12345";

if (xyz > 1)
    zipcode = "23456";

if (xyz > 2)
    zipcode = "34567";
那么你的情况现在是

(scr.CountryId == 0 && src.ZipCode == zipcode)

与其将条件设置为字符串,不如将其设置为表达式,这样您仍然可以在编译时验证语法是否有效:

Expression<Func<YourEntityType, bool>> Condition;

if (xyz > 0)
    Condition = scr => scr.ZipCode == 12345;
else if (xyz > 1)
    Condition = scr => scr.ZipCode == 23456;
else if (xyz > 2)
    Condition = scr => scr.SomeOtherField == "someStringValue";
else 
    Condition = scr => true; //or whatever makes sense as a default choice

var shippingShipCalculatorRecords = 
    _shippingShipCalculatorService.GetAllShippingShipCalculatorRecords()
            .Where(scr => scr.CountryId == 0)
            .Where(Condition)
            .OrderBy(x => x.Sequence).ToList();

不能从字符串执行代码。相反,请将邮政编码存储在字符串中。看起来它实际上并不是重复的,因为Servy的回答得到了OP-voting重新打开的积极反馈。@user3170203,我将问题编辑为更多关于构建条件的内容,这似乎是您的目标,而不是从字符串中解析条件。如果我的更改不符合您的实际问题,请随意编辑您的帖子。另外,如果Servy的答案是您所需要的,请接受它作为答案。这假设他添加的唯一过滤器是针对邮政编码的。将zipcodes表示为整数是有问题的,因为前导零很重要。如果使用九位数的Zipcode,情况会变得更糟。@SteveWellens好吧,这取决于它是如何存储在他这边的,我不知道,但过程保持完全相同,只是声明/赋值稍微改变以适应数据类型。在我的示例中反转了类型,但这并不重要。如果ZipCode字段用于存储国际邮政编码,情况会变得更糟。我们的答案中有字母,所以我们真的需要使用字符串,就像你在编辑后的答案中一样。+1。这是一种比解析字符串更安全的方法,因为它是完全强类型的。Servy读到了我的想法,因为我只将ZipCode用作表字段的示例,但条件可以基于任何其他字段SomeOtherField。在Condition=scr=>scr.ZipCode==的行中,我得到了字符串scr的错误。不包含ZipCode的定义?@user3170203那么,您在条件定义中定义的类型不是正确的类型,或者您的类型没有ZipCode属性,您最好现在就知道,这样您的代码在运行时就会失败。我的钱在前者身上;您可能输入了实体的基类型,或者输入了错误表的实体。@Servy ZipCode存在于表中,它是正确的字符串类型。然而,在创建条件之前,可能没有定义参数scr。它是在以下行中定义的:var shippingCalculatorRecords=等。如果是这样,我如何在创建条件之前定义它。再次感谢。