小编zar*_*han的帖子

使用 lambda 表达式查找计数

我有以下课程:

public class Promotion
{
    public Offers Offers { get; set; }
}
public class Offers
{
   public List<PromotionOffer> Offer { get; set; }
}

public class PromotionOffer
{
    public string CategoryName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个目标Promotion

Promotion applicablePromotion = promotion;
Run Code Online (Sandbox Code Playgroud)

applicablePromotion包含列表 Offer并且每个报价都有CategoryName。我想找到计数在哪里CategoryName == Package

就像是:

int count = applicablePromotion.Offers.Offer.Find(c => c.CategoryName == "Package").Count;
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c# linq lambda

6
推荐指数
1
解决办法
5931
查看次数

条件(三元)算子的条件

如何使用三元运算符实现此功能?

if(UnitType == null)
{
    a = ElevationType
}
else
{
    a = UnitType
}
Run Code Online (Sandbox Code Playgroud)

三元运算符

a = UnitType == null ? ElevationType : UnitType;
Run Code Online (Sandbox Code Playgroud)

现在我想要这样的东西

if(UnitType == null)
{
   if(ElevationType == null)
   {
    a = StructureType
   }
   else{
    a = ElevationType
   }
}
else
{
    a = UnitType
}
Run Code Online (Sandbox Code Playgroud)

我可以使用三元运算符实现这一目标吗?如果没有,该怎么办?

c# conditional if-statement ternary-operator

4
推荐指数
1
解决办法
135
查看次数

使用 lambda 表达式在列表中查找项目

我有以下课程:

public class AuthenticateCustomerResponse
{
    public EligiblePromotions EligiblePromotions { get; set; }
}

public class EligiblePromotions
{
    public List<PromotionList> PromotionList { get; set; }
}
public class PromotionList
{
    public string LineOfBusiness { get; set; }
    public AuthenticatePromotions Promotions { get; set; }
}
public class AuthenticatePromotions
{
    public List<AuthenticatePromotion> Promotion { get; set; }
}
public class AuthenticatePromotion
{
    public string ServiceType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想检索PromotionListLineOfBusiness等于“VID”且ServiceType等于“SAT”。我尝试了以下 lambda 表达式:

PromotionList getPromotion = …
Run Code Online (Sandbox Code Playgroud)

c# linq lambda

3
推荐指数
1
解决办法
1万
查看次数

找到包含特定文本的项目

我有下课

public class Response
{
   public Prompts Prompts { get; set; }
}
public class Prompts
{
    public List<Prompt> prompt { get; set; }
}
public class Prompt
{        
    public object message { get; set; }
    public object details { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想检索该消息prompt,其message包含"你好".但它给了我错误:

无法隐式地从提示转换为字符串

到目前为止,我尝试了这个lambda表达式:

string message = Response.Prompts.prompt.Find(p => p.message.ToString().Contains("Hello")); 
Run Code Online (Sandbox Code Playgroud)

c# linq lambda contains

0
推荐指数
1
解决办法
86
查看次数

通过ANY查找项目

我想指定prompt.message的值,其提示码为'2041'.我已经把if检查,但我不知道如何获得该提示项的消息.

班级结构

public class Prompts
{
    public List<Prompt> prompt { get; set; }
}

public class Prompt
{
    public string message { get; set; }
    public int code { get; set; }
} 
Run Code Online (Sandbox Code Playgroud)

到目前为止试过这个:

if(Prompts.prompt.Any(p => p.code == 2041))
{
    string message = Prompts.prompt[0].message;
}
Run Code Online (Sandbox Code Playgroud)

c# linq

0
推荐指数
1
解决办法
78
查看次数