Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ和Contains运算符连接两个集合_C#_Linq_Collections - Fatal编程技术网

C# 使用LINQ和Contains运算符连接两个集合

C# 使用LINQ和Contains运算符连接两个集合,c#,linq,collections,C#,Linq,Collections,我有以下两门课 Class C { public Guid Id{get;set;} public string Type{get;set;} } Class D { public List<Guid> CIds {get;set;} public string Type {get;set;} } var D_Collection = new List<D>(); var C_Collection = new List<C>(); // S

我有以下两门课

Class C
{
public Guid Id{get;set;}
public string Type{get;set;}
}

Class D
{
  public List<Guid> CIds {get;set;}
  public string Type {get;set;}
}

 var D_Collection = new List<D>();
 var C_Collection = new List<C>();

 // SET The C_Collection.Type On the Basis of D_Collection
 // If C.Id exist in D.CIds then C.Type = D.Type
试试这个代码

公共IQueryable GetData(字符串数据类型) {

IQueryable数据库数据=(
从db.All()中的t开始,其中(e=>e.Category==TransType)
加入WebHelpers.LocalList中的e
关于t型等于e型
orderby t.DATE降序发生
选择t
); 
返回dbData;
}

试试这段代码

公共IQueryable GetData(字符串数据类型) {

IQueryable数据库数据=(
从db.All()中的t开始,其中(e=>e.Category==TransType)
加入WebHelpers.LocalList中的e
关于t型等于e型
orderby t.DATE降序发生
选择t
); 
返回dbData;

}

如果联接条件是另一个集合上的
包含
,则可以使用
可枚举的.Join
。您可以首先在
Guid
-集合上使用
SelectMany
from
..
from
在查询语法中):

var allDIDs = from d in D_Collection from id in d.CIds select new { dObj = d, id };
var cToUpdate = from c in C_Collection
                join dID in allDIDs
                on c.Id equals dID.id
                select new { cObj = c, dType = dID.dObj.Type };

foreach (var x in cToUpdate.Distinct())
    x.cObj.Type = x.dType;
或使用此查询:

var cToUpdate = C_Collection
    .Select(c => new { 
        cObj = c, 
        FirstmatchingIdType = D_Collection
          .FirstOrDefault(d => d.CIds.Contains(c.Id))?.Type 
     })
    .Where(x => x.FirstmatchingIdType != null);

foreach(var x in cToUpdate)
    x.cObj.Type = x.FirstmatchingIdType;

如果联接条件是另一个集合上的
包含
,则可以使用
可枚举.联接
。您可以首先在
Guid
-集合上使用
SelectMany
from
..
from
在查询语法中):

var allDIDs = from d in D_Collection from id in d.CIds select new { dObj = d, id };
var cToUpdate = from c in C_Collection
                join dID in allDIDs
                on c.Id equals dID.id
                select new { cObj = c, dType = dID.dObj.Type };

foreach (var x in cToUpdate.Distinct())
    x.cObj.Type = x.dType;
或使用此查询:

var cToUpdate = C_Collection
    .Select(c => new { 
        cObj = c, 
        FirstmatchingIdType = D_Collection
          .FirstOrDefault(d => d.CIds.Contains(c.Id))?.Type 
     })
    .Where(x => x.FirstmatchingIdType != null);

foreach(var x in cToUpdate)
    x.cObj.Type = x.FirstmatchingIdType;

这是一个简单的迭代过程,很可能有更好的解决方案。然而,它会做你需要的

public void SetTypeOnCList(List<D> dlist, List<C> clist)
{
    clist.ForEach(c =>
    {
        var dobj = dlist.FirstOrDefault(d => d.CIds.Contains(c.Id));
        if (dobj != null)
        {
            c.Type = dobj.Type;
        }
    });
}
public void SetTypeOnCList(List-dlist,List-clist)
{
clist.ForEach(c=>
{
var dobj=dlist.FirstOrDefault(d=>d.CIds.Contains(c.Id));
如果(dobj!=null)
{
c、 Type=dobj.Type;
}
});
}

这是一个简单的迭代过程,很可能有更好的解决方案。然而,它会做你需要的

public void SetTypeOnCList(List<D> dlist, List<C> clist)
{
    clist.ForEach(c =>
    {
        var dobj = dlist.FirstOrDefault(d => d.CIds.Contains(c.Id));
        if (dobj != null)
        {
            c.Type = dobj.Type;
        }
    });
}
public void SetTypeOnCList(List-dlist,List-clist)
{
clist.ForEach(c=>
{
var dobj=dlist.FirstOrDefault(d=>d.CIds.Contains(c.Id));
如果(dobj!=null)
{
c、 Type=dobj.Type;
}
});
}

我认为这样做更简单:

var c_comparations=D_Collection.SelectMany(x=>x.CIds.Select(y=>newc{Id=y,Type=x.Type}));
foreach(c_比较中的var比较)
{
var c=c_Collection.FirstOrDefault(x=>x.Id==comparison.Id);
如果(c!=null)
{
c、 类型=比较。类型;
}
}

我认为这样做更简单:

var c_comparations=D_Collection.SelectMany(x=>x.CIds.Select(y=>newc{Id=y,Type=x.Type}));
foreach(c_比较中的var比较)
{
var c=c_Collection.FirstOrDefault(x=>x.Id==comparison.Id);
如果(c!=null)
{
c、 类型=比较。类型;
}
}

你想把两个列表合并成一个
列表
吗?任何让我在D.types的基础上设置C.Type的东西你想把两个列表合并成一个
列表
吗?任何让我在D.types的基础上设置C.Type的东西在一个集合中它的列表在另一个集合中它的Guid如何工作这里?这是一个集合中的列表,另一个集合中的Guid,equals在这里如何工作?这是正确的,糟糕的命名约定:P。这就是我们进行对等会话重构的原因;)没错,这里的命名约定很糟糕:P。这就是为什么我们有对等会话重构;)