从列表(类)中创建一个带有条件的动态列表

时间:2015-09-15 06:52:22

标签: vb.net linq

我有一个名为UserDetail的类。我在userdetail类中有Userstatus字段并创建了一个List(Of UserDetail)。


Dim UserList As New List(Of UserDetail)()

UserList.Add(New UserDetail() With {.UserId = 1, .UserName = 'ABC', .Status= 0 })
UserList.Add(New UserDetail() With {.UserId = 2, .UserName = 'CFC', .Status= 2 })
UserList.Add(New UserDetail() With {.UserId = 3, .UserName = 'AAC', .Status= 2 })

我想迭代List中的所有userdetail,并将status = 1的所有内容都添加到一个新的列表中,只包含UserID。

如何使用最小步骤进行此操作..

2 个答案:

答案 0 :(得分:1)

您可以使用 linq 使用#include<vector> #include<iostream> using namespace std; #define pii pair<int,int> #define F first #define S second #define MP make_pair int calc2(int a){ int c=0; while(a%2==0){ c++; a/=2; } return c; } int calc5(int a){ int c=0; while(a%5==0){ c++; a/=5; } return c; } int mini(int a,int b){ return a<b?a:b; } pii min(pii a, pii b){ if(mini(a.F,a.S) < mini(b.F,b.S)) return a; return b; } int main(){ int n; cin>>n; vector<vector<pii > > v; vector<vector<int> > path; int i,j; for(i=0;i<n;i++){ vector<pii > x; vector<int> q(n,0); for(j=0;j<n;j++){ int y;cin>>y; x.push_back(MP(calc2(y),calc5(y))); //I store factors of 2,5 in the vector to calculate } x.push_back(MP(100000,100000)); //padding each row to n+1 elements (to handle overflow in code) v.push_back(x); path.push_back(q); //initialize path matrix to 0 } vector<pii > x(n+1,MP(100000,100000)); v.push_back(x); //pad 1 more row to handle index overflow for(i=n-1;i>=0;i--){ for(j=n-1;j>=0;j--){ //move from destination to source grid if(i==n-1 && j==n-1) continue; //here, the LHS of condition in if block is the condition which determines minimum number of trailing 0's. This is the same condition that is used to manipulate "v" for getting the same result. if(min(MP(v[i][j].F+v[i+1][j].F,v[i][j].S+v[i+1][j].S), MP(v[i][j].F+v[i][j+1].F,v[i][j].S+v[i][j+1].S)) == MP(v[i][j].F+v[i+1][j].F,v[i][j].S+v[i+1][j].S)) path[i][j] = 1; //go down else path[i][j] = 2; //go right v[i][j] = min(MP(v[i][j].F+v[i+1][j].F,v[i][j].S+v[i+1][j].S), MP(v[i][j].F+v[i][j+1].F,v[i][j].S+v[i][j+1].S)); } } cout<<mini(v[0][0].F, v[0][0].S)<<endl; //print result for(i=0,j=0;i<=n-1 && j<=n-1;){ //print path (I don't know o/p format) cout<<"("<<i<<","<<j<<") -> "; if(path[i][j]==1) i++; else j++; } return 0; } 应用过滤器,并使用.Where

获取UserId列表

C#语法

.Select

VB语法

var result = UserList.Where(u=>u.status == 1).Select(u=>u.UserId);

这篇MSDN文章Introduction to LINQ in Visual Basic将帮助您使用VB.net学习LinQ

答案 1 :(得分:0)

您可以使用LINQ迭代您的用户列表:

    document.getElementById("set").onclick = function() {
    var d = document.getElementById("text").value;
    chrome.storage.sync.set({ "data" : d }, function() {
        if (chrome.runtime.error) {
            console.log("Runtime error.");
        }
    });
    window.close();
}