1

我有树对象。

一个对象EFacebook,非常简单,带有一个名为 的属性id

class EFacebook
{
   public string id { get; set; }
}

A EUser,更复杂。EUser得到一个内部列表,EProvider每个EProvider都有一个类型。像:

class EUser
{
    public string name { get; set; }
    public List<EProvider> EProviders { get; set; }
}

class EProvider
{
   public enum EnumType
   {
      facebook = 1,
      twitter = 2
   }
   public string id { get; set; }
   public EnumType type { get; set; }
}

还有两个列表:(提供者列表,内部用户,是可选的,可为空的):

// list of facebooks
List<EFacebook> facebooks = new List<EFacebook>();

facebooks.Add(new EFacebook { id = "1" });
facebooks.Add(new EFacebook { id = "2" });
facebooks.Add(new EFacebook { id = "3" });

// list of users
List<EUser> users = new List<EUser>();
List<EProvider> ps1 = new List<EProvider>();

ps1.Add(new EProvider { id = "1", type = EProvider.EnumType.facebook });
ps1.Add(new EProvider { id = "2", type = EProvider.EnumType.twitter });

List<EProvider> ps2 = new List<EProvider>();
ps2.Add(new EProvider { id = "3", type = EProvider.EnumType.facebook });
ps2.Add(new EProvider { id = "4", type = EProvider.EnumType.twitter });

EUser u1 = new EUser { name = "somea", EProviders = ps1 };
EUser u2 = new EUser { name = "someb", EProviders = ps2 };
EUser u3 = new EUser { name = "somec" };

users.Add(u1);
users.Add(u2);
users.Add(u3);

现在,我需要根据和facebooks之间的关系获取两个子列表。这意味着,一个只包含 2 的对象的列表,因为我没有一个类型为 2 的提供者的用户,以及一个包含 1 和 3 的对象的列表,因为我确实有一个类型为 a 的用户和1 和3。facebook.iduser.eprovider(of enum facebook).idEFacebookfacebookidfacebookidEFacebookfacebookidproviderfacebookidid

我尝试了很多,但它不起作用:

/*var xaa = users.Select(z => z.EProviders.Where(x => x.ProviderType == 
    EProvider.EnumProviderType.Facebook).Select(x => x.Ip).Zip(z);*/

/*var a = users.Select(x => x.EProviders.Where(y => y.ProviderType == 
    EProvider.EnumProviderType.Facebook));*/


/*
var outra = facebooks.Where(x=>                 
var a = from i in users
    where i.EProviders != null && i.EProviders.Any(j => j.ProviderType == 
    EProvider.EnumProviderType.Facebook)
from j in i.EProviders
join k in facebooks on j.Ip equals k.id
select k;

var b = facebooks.Except(a);
*/

任何帮助将不胜感激。

4

1 回答 1

2

怎么样:

// Create an enumerable of all IDs of Facebook providers from the users list
var facebookIds = users
    // Exclude all users with a null EProviders list
    .Where(u => u.EProviders != null)
    // For each user, select all EProviders with type == facebook
    // and use SelectMany to flatten them into a single enumerable
    .SelectMany(u => u.EProviders.Where(p => p.type == EProvider.EnumType.facebook));

// Use Join to find all facebooks whose IDs also exist in the facebookIds set constructed above
var facebooksWithUsers = facebooks.Join(facebookIds, f => f.id, p => p.id, (f, p) => f);

// Use Except to find the opposite subset
var facebooksWithoutUsers = facebooks.Except(facebooksWithUsers);

// Write the contents of the two sets to the console
Console.WriteLine("facebooksWithUsers:");
foreach (var fb in facebooksWithUsers)
{
    Console.WriteLine(fb.id);
}

Console.WriteLine();
Console.WriteLine("facebooksWithoutUsers:");
foreach (var fb in facebooksWithoutUsers)
{
    Console.WriteLine(fb.id);
}

这输出:

facebooksWithUsers:
1
3

facebooksWithoutUsers:
2
于 2012-04-12T16:18:17.720 回答