【问题标题】:How to work where conditions simultaneously in a LINQ C#如何在 LINQ C# 中同时工作
【发布时间】:2017-10-26 07:40:57
【问题描述】:
Page curpage = new Page();
History history = (History)curpage.Session["history"];

List<TreatmentLedger> hasvalidrows = 
 (from h in history.Rows where h.Status != 11 && h.displayStatus != "" select h).ToList();

它不工作我需要没有status!=11 and displayStatus !=""的行

如果status==11displayStatus =="unsaved"。它必须是有效行。

如果status==11displayStatus =="".它必须是无效行。

如果status!=11displayStatus =="unsaved".它必须是有效行。

如果status!=11displayStatus =="".它必须是有效行。 请帮助我。

    List<TreatmentLedger> hasvalidrows = (from h in history.Rows where (h.Status != 11 || h.displayStatus != "")  select h).ToList();--its working

但现在我有另一个条件

如果 status==11displayStatus =="unsaved" 并且 id !=0。它必须是有效行。 if status==11 and displayStatus =="unsaved" and id ==0.it must be a invalid row.

如果status==11displayStatus =="".它必须是无效行。

如果status!=11displayStatus =="unsaved".它必须是有效行。

如果status!=11displayStatus =="".它必须是有效行。

【问题讨论】:

  • 所以在你的 where 子句中写下所有这些......

标签: c# asp.net linq


【解决方案1】:

如果 status==11 和 displayStatus =="unsaved"。它必须是有效的行。

那么你需要使用逻辑OR

List hasvalidrows = (from h in history.Rows 
                     where h.Status != 11 || h.displayStatus != "" select h).ToList();

这样,一个或另一个必须至少有效才能使整行有效

【讨论】:

  • 它的工作。如果我有这样的另一种情况。如果 status==11 并且 displayStatus =="unsaved" 并且 id !=0。它必须是有效的行。如果 status==11 和 displayStatus =="unsaved" 和 id ==0。它必须是无效行
  • @shalushaji 我很高兴它对你有用。有时可能会令人困惑。因为我们倾向于不总是以清晰的逻辑方式使用自然语言。您可以将其扩展为包括 id != 0 但要小心它是 AND 还是 OR
  • List hasvalidrows = (from h in history.Rows where h.Status != 11 || h.displayStatus != "" || h.id !=0 select h).ToList ();它不起作用。我无法理解这背后的逻辑。通常我们使用 && -- 表示需要两个条件都为真和 ||--- 任何一个为真,它返回真。
  • 看看你的例子“如果 status==11 and displayStatus =="unsaved".it must be a valid row.”仅满足 2 个条件中的一个,并且您有一个有效的行!所以你需要使用或。如果id !=0 是强制性的,则需要将其与其余部分与 AND 结合起来,如下所示:where (h.Status != 11 || h.displayStatus != "") &amp;&amp; h.id !=0 select h
  • 它不工作列表 hasvalidrows = (from h in history.Rows where (h.Status != 11 || h.displayStatus != "") && h.id !=0 select h).ToList();如果 status==0 和 dispalystatus="unsaved" 和 id=0 它什么也不返回
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
相关资源
最近更新 更多