使用Linq过滤分组产品

时间:2017-01-31 19:02:21

标签: c# linq group-by

我有一个 productInfo 类,它具有 ProductId 区域设置属性,并尝试显示具有当前区域设置的产品(id) ,并显示具有相同ID的产品。

这意味着,在按产品ID分组后,一旦某个产品的区域设置等于当前区域设置,我们就会显示该组(具有各自的区域设置)。

以下是seudo代码/示例:

Option Explicit

Sub ConvertFormulaToVBA()

Dim MatchRow As Variant
Dim Rng     As Range
Dim lengthRows As Long, i As Long

lengthRows = Cells(Rows.Count, "H").End(xlUp).Row '<-- get last row with data in Column H (in your example it's 10)
Set Rng = Range("H2:H" & lengthRows) ' <-- set the range to H2 until last row in Column H        

MatchRow = Application.Match(0.01, Rng, -1) ' <-- setting the third parameter to -1, meaning greater than 0.01
If Not IsError(MatchRow) Then
    Range("AC2").Value = Range("X" & MatchRow + Rng.Item(0).Row).Value
Else
    ' raise a message box if there is no Match
    MsgBox "No none-zero value found at Range " & Rng.Address
End If

End Sub

如果当前区域设置等于1,则最终列表应包含:

 public class productInfo
{
 public int ProductId {get;set;}
 public int LocaleId  {get;set;}
}


List<productInfo> listProduct = List<productInfo>();
listProduct.Add(new productInfo{ ProductId = 100, LocaleId = 1 });
listProduct.Add(new productInfo{ ProductId = 100, LocaleId = 2 });
listProduct.Add(new productInfo{ ProductId = 100, LocaleId = 3 });
listProduct.Add(new productInfo{ ProductId = 200, LocaleId = 4 });
listProduct.Add(new productInfo{ ProductId = 300, LocaleId = 1 });
listProduct.Add(new productInfo{ ProductId = 300, LocaleId = 5 });
listProduct.Add(new productInfo{ ProductId = 400, LocaleId = 6 });

将从列表中删除以下内容:

productInfo { ProductId = 100, LocaleId = 1 }
productInfo { ProductId = 100, LocaleId = 2 }
productInfo { ProductId = 100, LocaleId = 3 }
productInfo { ProductId = 300, LocaleId = 1 }
productInfo { ProductId = 300, LocaleId = 5 }

如何使用Linq实现这一目标?

1 个答案:

答案 0 :(得分:1)

  var siteGlobal = (function(){

            init();

            var init = function(){
               bindEvents();
            }

             // then i got my function to bind events
             var bindEvents = function(){
                $(document).on('mousewheel', mouseNav());
                $(document).on('keyup', mouseNav());
             }

             // then i got my function here for capture the event
             var mouseNav = function(){
               // the code here for capturing direction or keyboard
               // and then check next section
             }

             var nextSection = function(){
               // Here we check if there is prev() or next() section
               // if there is do the change on the section
             }

            var switchSection = function(nextsection){
              // Get the current section and remove active class
              // get the next section - add active class
              // get the name of the function with data-name attribute
              // trow the animation 
              var funcEnter = window['section'+ Name + 'Enter'];
            } 


           // Let's pretend section is call Intro 
           var sectionIntroEnter = function(){
              // animation code here
           }

          var sectionIntroExit = function(){
             // animation code here
           }
     }();

将1替换为当前区域设置。