Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 创建列表<;T>;首先匹配项,然后使用LINQ添加其余项_C#_Linq - Fatal编程技术网

C# 创建列表<;T>;首先匹配项,然后使用LINQ添加其余项

C# 创建列表<;T>;首先匹配项,然后使用LINQ添加其余项,c#,linq,C#,Linq,这应该很容易 我遇到了一个问题,希望有人能帮我解决。假设我有一门课: public class Product { public int ProductID {get; set;} // Primary key public int CategoryID {get; set; } // Foreign key } 假设我需要找到列表中的所有产品。其中(p=>p.CategoryID==2),然后列出其余的产品 换言之,任何与类别匹配的产品我都需要先列出它

这应该很容易

我遇到了一个问题,希望有人能帮我解决。假设我有一门课:

public class Product
{
    public int ProductID {get; set;}        // Primary key
    public int CategoryID {get; set; }      // Foreign key
}
假设我需要找到
列表中的所有产品。其中(p=>p.CategoryID==2)
,然后列出其余的产品

换言之,任何与类别匹配的产品我都需要先列出它们,然后列出其他不匹配的产品


有什么建议吗?

您可以使用
方法,但
方法除外:

var categoryTwoItems = products.Where(p => p.CategoryID == 2);
var otherItems = products.Except(categoryTwoItems);
如果您想按类别Id对所有项目进行排序,但让
CategoryId==2的项目排在第一位,您可以执行以下操作:

var catId2First = products
    .OrderByDescending(p => p.CategoryID == 2)
    .ThenBy(p => p.CategoryID)
    .ToList();

您可以使用除
以外的
方法:

var categoryTwoItems = products.Where(p => p.CategoryID == 2);
var otherItems = products.Except(categoryTwoItems);
如果您想按类别Id对所有项目进行排序,但让
CategoryId==2的项目排在第一位,您可以执行以下操作:

var catId2First = products
    .OrderByDescending(p => p.CategoryID == 2)
    .ThenBy(p => p.CategoryID)
    .ToList();

您可以使用Exception和2个查询

var categoryProducts = products.Where (p => p.CategoryId == 2).ToArray();
var others = products.Except(categoryProducts);
或者,您也可以一次性使用groupBy

   var productGroup= product.GroupBy(p => p.CategoryId == 2);
   var category2Products = productGroups.First(p => p.Any(x=> x.CategoryId==2));
   var others = productGroups.Except(category2Products); 

您可以使用Exception和2个查询

var categoryProducts = products.Where (p => p.CategoryId == 2).ToArray();
var others = products.Except(categoryProducts);
或者,您也可以一次性使用groupBy

   var productGroup= product.GroupBy(p => p.CategoryId == 2);
   var category2Products = productGroups.First(p => p.Any(x=> x.CategoryId==2));
   var others = productGroups.Except(category2Products); 
您还可以使用:

ILookup categorizedProducts=products.ToLookup(p=>(p.CategoryId==2));
IEnumerable category2Products=分类产品[true];
IEnumerable nonCategory2Products=分类产品[false];
您还可以使用:

ILookup categorizedProducts=products.ToLookup(p=>(p.CategoryId==2));
IEnumerable category2Products=分类产品[true];
IEnumerable nonCategory2Products=分类产品[false];

您可以通过排序:

List<Products>.OrderByDescending(p => p.CategoryID == 2);
List.OrderByDescending(p=>p.CategoryID==2);

您可以通过排序:

List<Products>.OrderByDescending(p => p.CategoryID == 2);
List.OrderByDescending(p=>p.CategoryID==2);

尝试以下操作:ListSelect(p=>order=p.CategoryID==2?1:2)。OrderBy(x=>x.order)需要什么格式的结果?只需再次列出一个列表?请尝试以下操作:ListSelect(p=>order=p.CategoryID==2?1:2)。OrderBy(x=>x.order)需要什么格式的结果?又是一张单子?