如何从通用列表的特定索引中查找不同的值

问题描述 投票:0回答:1

我有一个通用列表

LinkedList<RelationModel> mylist = new LinkedList<RelationModel>();

此列表以两个索引{0}和{1}获取输出。两个索引都有多个值。在调试时,在

{0} - the output is     

Foreign             "Store"       --string
ForeignKeyColumn    "addressId"   --string
Primary             "Address"     --string
PrimaryKeyColumn    "addressId"   --string

{1} - the output is 

Foreign             "Address"     --string
ForeignKeyColumn    "countryId"   --string
Primary             "Country"     --string
PrimaryKeyColumn    "countryId"   --string

问题是我必须从列表中选择不同的值。例如 - 这里,商店,地址和国家是不同的。如何在C#代码中选择这些值?

以下代码将数据填充到mylist中

using (var ctx = new TestDBEntities())
            {
                var foreginKeyList = ctx.Database
                .SqlQuery<MetdataModel>(" Select  [PKTABLE_NAME], [PKCOLUMN_NAME],[FKTABLE_NAME],[FKCOLUMN_NAME] from GetMetadata")
                .ToList();
                LinkedList<RelationModel> mylist = new LinkedList<RelationModel>();
                for (int i =0; i < foreginKeyList.Count; i++)
                {

                    var ss = foreginKeyList.Where(p => p.FKTABLE_NAME.Equals(foreginKeyList[i].FKTABLE_NAME)).Select(p => new { p.PKTABLE_NAME, p.PKCOLUMN_NAME }).ToList();
                    foreach(var col in ss)
                    {
                        RelationModel gg = new RelationModel();
                        gg.Foreign = foreginKeyList[i].FKTABLE_NAME;
                        gg.ForeignKeyColumn = foreginKeyList[i].FKCOLUMN_NAME;
                        gg.Primary = col.PKTABLE_NAME;
                        gg.PrimaryKeyColumn = col.PKCOLUMN_NAME;
                        mylist.AddLast(gg);

                    }

                }

我需要一个具有不同值的最终列表

mylist.[0]- store
mylist.[1]- address
mylist.[2]- country
c# linq collections generic-list
1个回答
0
投票

假设RelationModel

public class RelationModel {
    public string Foriegn;
    public string ForeignKeyColumn;
    public string Primary;
    public string PrimaryKeyColumn;
}

然后你可以使用以下内容:

var ans = mylist.SelectMany(m => new[] { m.Foriegn, m.Primary }).Distinct().ToList();

现在你可以访问ans的元素作为ans[0]ans[1]等。

© www.soinside.com 2019 - 2024. All rights reserved.