开发者

How to write simple code in LInq

开发者 https://www.devze.com 2022-12-29 03:11 出处:网络
I have the following in my NotSelectedList. public List<TestModel> SelectedList = new List<TestModel>();

I have the following in my NotSelectedList.

public List<TestModel> SelectedList = new List<TestModel>();
public List<TestModel>NotSelectedList =  new List<TestModel>();
NotificationDetailsModel projects = new NotificationDetailsModel();
projects.ProjectID = Convert.ToInt32(Row["ProjectID"]);
projects.Valid= Convert.ToBoolean(Row["Validity"]);
NotSelectedList.Add(projects);

How can I write a simple code in LINQ to select from the NotSelectedList where Validity ==开发者_如何学编程 True and store the data in SelectedList?


var query = from ns in NotSelectedList
            from n in SelectedList
            where ns.Valid && ns.ProjectID == n.ProjectID
            select ns;

Hope this will help you


The following would select the items with Validity = true from NotSelectedList and place them in SelectedList:

SelectedList.AddRange(NotSelectedList.Where(item => item.Validity));


Try this:

var results = NotSelectedList.Where(x => x.Valid);
foreach (var item in results)
    SelectList.Add(item);

Although for performance reasons you may be better off doing something like this:

foreach (var item in NotSelectedList)
{
    if (item.Valid)
        SelectList.Add(item);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消