简体   繁体   English

从对象列表中选择匹配的ID

[英]Selecting a matching ID from a list of objects

Okay so i have a list filled with objects which have the following properties: 好的,所以我有一个列表,其中填充了具有以下属性的对象:

category_id and category_title category_idcategory_title

In the code it looks like this: 在代码中,它看起来像这样:

class CategoryModel
{
    public int category_id { get; private set; }
    public string category_title { get; private set; }

    //Constructor
    public CategoryModel(int id, string title)
    {
        category_id = id;
        category_title = title;
    }

I have multiple objects of this class stored in a list 我有一个此类的多个对象存储在列表中

    static List<CategoryModel> CategoryList = new List<CategoryModel>();

From this list i am trying to select the category_title where it matches a certain category_id 从这个列表中我试图选择category_title它一定的匹配CATEGORY_ID

    public static string GetCategoryName(int categoryNumber)
    {
        IEnumerable<string> title = from c in CategoryList
                    where c.category_id.Equals(categoryNumber)
                    select c.category_title;
        return title.ToString();
    }

So let's say GetCategoryName(4), it's now supposed to select from the list where category_id equals 4 and return category_title 假设GetCategoryName(4),现在应该从category_id等于4的列表中选择并返回category_title

However it seems that i'm not getting the name back but a syntax of the query instead! 但是,似乎我没有找回名字,而是查询的语法!

Output: system.linq.enumerable.whereselectlistiterator'2[project.CategoryModel,System.string] 输出: system.linq.enumerable.whereselectlistiterator'2 [project.CategoryModel,System.string]

Just use Linq to do it: 只需使用Linq即可:

return CategoryList.Single(c => c.category_id == categoryNumber).category_title;

Single will throw an exception if the category list does not have any matching ID. 如果类别列表没有任何匹配的ID,则Single将引发异常。

Your query returns list not a single category. 您的查询返回列表不是单个类别。

Please try this 请尝试这个

public static string GetCategoryName(int categoryNumber)
{
    IEnumerable<string> title = from c in CategoryList
                where c.category_id.Equals(categoryNumber)
                select c.category_title;
    return title.FirstOrDefault();
}

The IEnumerable class does not implement some sort of fancy format of its elements in the ToString function. IEnumerable类未在ToString函数中实现其元素的某种特殊格式。 I think the simplest way to approach this would be to use the String.Join function if there are supposed to be multiple results: 我认为解决此问题的最简单方法是,如果应该有多个结果,则使用String.Join函数:

return String.Join(",", title);

You should use Linq to select first or default value : 您应该使用Linq选择第一个或默认值:

public static string GetCategoryName(int categoryNumber) {
    return CategoryList.FirstOrDefault( category => category.category_id == categoryNumber).category_title;
}

But this can throw exception when there's no such category id because of .category_title so the best solution would be : 但是当由于.category_title而没有此类类别ID时,这可能会引发异常,因此最佳解决方案是:

public static string GetCategoryName(int categoryNumber) {
    CategoryModel category = CategoryList.FirstOrDefault( category => category.category_id == categoryNumber);
    return category == null ? string.Empty : category.category_title;
}

Which will return empty string when no such category was found. 如果找不到此类,它将返回空字符串。

About the error you're getting back. 关于错误,您要回来。 It is because your query returns "many" objects of CategoryModel ie. 这是因为您的查询返回了CategoryModel即“很多”对象。 IEnumerable<string> which then you try to convert to string. IEnumerable<string> ,然后尝试将其转换为字符串。

Doing ToString() on any kind of table ( array, list, etc. ) will return something like system.string[] or in other words same result as you would do call IEnumerable<string> .ToString() . 在任何类型的表(数组,列表等ToString()上执行ToString()都将返回诸如system.string[] ,换句话说,与调用IEnumerable<string> .ToString()

public static string GetCategoryName(int categoryNumber)
{
    CategoryModel result = CategoryList.Where(x => x.category_id == categoryNumber).FirstOrDefault();
    if (result != null)
        return result.category_name;
    else
        //no matches, do something else
}

This will take the item from CategoryList where the category_id matches the number you passed in, and return category_name if there was a match. 这将从CategoryList中的项目中获得,其中category_id与您传入的数字匹配,如果匹配则返回category_name。

You're ToString'ing the Enumerable, which hasn't been evaluated, which is why you're getting the query implementation object. 您正在对尚未枚举的Enumerable进行ToString'ing,这就是为什么要获取查询实现对象的原因。

Assuming you're after the only item that matches, do: 假设您是唯一匹配的商品,请执行以下操作:

title.SingleOrDefault()?.ToString()

NOTE: the ? 注意: ? operator will only work on C# 6; 操作员只能在C#6上工作; you can just do a normal assignment and if x == null type check for below 您可以进行常规分配, if x == null请检查以下内容

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

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