简体   繁体   English

如何在 LINQ 和 C# 中使用 if 条件

[英]How to use an if condition in LINQ with C#

在此处输入图像描述 I want to display value of one variable whose value depend on another variable.我想显示一个变量的值,其值取决于另一个变量。 let say if I have three rows in a table:假设我在表中有三行:

starttime   endtime  comment
02:55       15:56    abc
03:54       23:46    def
09:33       12:69    oiuy

I want to use Where condition in Linq such that comment should be display on the basis of starttime, so that if I select 02:55 , then comment should display abc .我想在 Linq 中使用Where条件,这样评论应该基于开始时间显示,这样如果我02:55 ,那么评论应该显示abc

i use this linq query:我使用这个 linq 查询:

Comment = fr.Where(x => x.Starttime).Comment

but it is not correct.但这是不正确的。 Can anyone suggest what improvement I need?谁能建议我需要什么改进?

The Where clause must return a bool , so all you're missing is the actual comparison: Where子句必须返回一个bool ,所以你所缺少的只是实际的比较:

fr.Where(x => x.Starttime == "02:55")

But now we also need to decide what to select.但是现在我们还需要决定 select 是什么。 There may be many, one, or no items returned.可能有很多、一个或没有退回的物品。 If we only want the first match, we can use FirstOrDefault , which will return the first record, or null if none are returned.如果我们只想要第一个匹配,我们可以使用FirstOrDefault ,它将返回第一条记录,或者null如果没有返回。 We can also select a default value, like "[no match found]" if no records are returned, by comparing the result of FirstOrDefault to null (using the ?? operator):我们还可以通过将 FirstOrDefault 的结果与FirstOrDefault进行null (使用??运算符),将 select 设置为默认值,例如"[no match found]"如果没有返回记录:

var comment = fr
    .Where(item => item.Starttime == "02:55")
    .FirstOrDefault()
    ?.Comment ?? "[no match]";

If we want to select many comments, then we can use the Select method to specify the property we want to select, and ToList() to return them all in a List<string> :如果我们想要 select 多条评论,那么我们可以使用Select方法指定我们想要的属性 select 和ToList()将它们都返回到一个List<string>中:

var allComments = fr
    .Where(item => item.Starttime == "02:55")
    .Select(item => item.Comment)
    .ToList();

Update更新

Now that you've posted a screenshot of your code (which is not recommended here - better to paste the relevant code into a code section in your question), it appears that you're setting properties of an instance of an anonymouse type based on the properties of items in an IGrouping .现在您已经发布了代码的屏幕截图(此处不建议这样做 - 最好将相关代码粘贴到问题中的代码部分),看来您正在设置基于匿名类型的实例的属性IGrouping中项目的属性。

Since you have StartTime = fr.Min(x => x.StartTime) , and apparently the Comment is somehow related to StartTime , you may want to do something like this:由于您有StartTime = fr.Min(x => x.StartTime) ,并且显然CommentStartTime有某种关系,您可能想做这样的事情:

// Get the Comment from the item with the smallest (Min) StartTime
Comment = fr.OrderBy(x => x.StartTime).FirstOrDefault()?.Comment ?? "[no comment]",

Try尝试

var comments = fr.Where(x => x.Starttime == val).Select(x=> x.Comment);

If you only want the first match, try如果您只想要第一场比赛,请尝试

 var comment = fr.Where(x => x.Starttime == val).FirstOrDefault()?.Comment;

or more concisely或更简洁地说

var comment = fr.FirstOrDefault(x => x.Starttime == val)?.Comment;

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

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