简体   繁体   English

如何通过使用LINQ使用另一个列表中的数据来构建列表

[英]How to build List by using data from another List using LINQ

i have some List that hold 1 or more Guid. 我有一些持有1个或更多Guid的列表。

var getMyfirstListGuids = (from dlist in db.MyTable
    where dlist.id == theid
    select dlist).ToList();

List<MyfirstList> myfirstList = new List<MyfirstList>();

foreach (var item in myLandingPageList)
{
    Guid theguid = new Guid(item.guid);
    MyfirstList addnewRow = new MyfirstList();
    addnewRow.LpGuid = new Guid(theguid);
    myfirstList.Add(addnewRow);
} 

Now i have a list with 1 or more guids. 现在,我有一个包含1个或多个向导的列表。 my next step is to make list with data from SQL by the first list guids. 我的下一步是通过第一个列表向导使用来自SQL的数据创建列表。 In the SQL could be 1 row or more for each guid. 在SQL中,每个guid可能是1行或更多行。 with one row result i can guess what to do. 仅有一行结果,我可以猜测该怎么办。 but if there is many results i dont have an idea. 但是,如果有很多结果,我不知道。

Okay so you wanna do it like this: 好的,所以您想这样做:

List<SecondListGUID> secondListGUID = new List<SecondListGUID>();
foreach (var item in myfirstList)
{
    for(int i = 0; i<_yourDBEntity.GUIDs.Count(); i++)
    {
        if(item.LpGuid == _yourDBEntity.GUIDs[i].GUID)
        secondListGUID.add(
            new SecondListGUID() {
                // add the corresponding GUID's here 
        });
    }
}

Basically you have to do a foreach through your first List and then do a for loop (or foreach - whichever you prefer) through your DB table (entity in this case if you're using Entity framework) and simply compare the GUID's from your DB table and if they match you'll wanna add the item to your third list. 基本上,您必须通过第一个List进行一次foreach,然后通过您的数据库表(在这种情况下,如果使用的是实体),在数据库表中进行for循环(或foreach-无论您喜欢使用哪种方式),然后只需比较数据库中的GUID表格,如果它们匹配,您要将其添加到您的第三个列表中。

PS I've worked with the informations that you've provided, you can change the 2nd list type into the one you need, and change your entity framwork data model name into the one you actually use :) PS我已经处理了您提供的信息,可以将第二个列表类型更改为所需的类型,并将实体框架数据模型名称更改为实际使用的类型:)

You can try this 你可以试试这个

List<Guid>getMyfirstListGuids =  new List<Guid>();

getMyfirstListGuids.addRange(from dlist in db.MyTable
    where dlist.id == theid
    select dlist).ToList());

List<MySecList> mySecList = new List<MySecList>();

mySecList.AddRange(_db.myLandingPageList.Where(p => p.Guid.Any(x => getMyfirstListGuids.Contains(x));

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

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