-1

我编辑我的帖子,因为假设 3 个方法从数据库表返回行是docTagstagsdocs. docTags 是一个中间表。一个doc(文档)可以有多个tags,一个tag可以属于多个docs 我怎样才能得到:TagKey = "123" 在哪里 docTags.Add(new Configuration(1, 1));

    class Program
    {

        static void Main(string[] args)
        {

        }
    }

    public class Configuration
    {
        public  int  DocID { get; set; }
        public int TagID { get; set; }
        public string Name { get; set; }
        public string DocKey { get; set; }
        public string TagKey { get; set; }

        public Configuration(int _docId,string _name, string _docKey)
        {
            DocID = _docId;
            Name = _name;
            DocKey = _docKey;
        }
        public Configuration(int _tagId,string name, string _dockey,string _tagKey)
        {
            TagID = _tagId;
            Name = name;
            TagKey = _tagKey;
        }
        public Configuration(int _tagId, int _docId)
        {
            TagID = _tagId;
            DocID = _docId;
        }
        public static List<Configuration> getDocType()
        {
            List<Configuration> docs = new List<Configuration>();
            docs.Add(new Configuration(1,"contract", "xxx"));
            docs.Add(new Configuration(2,"Action", "yyy"));
            return docs;
        }

        public static List<Configuration> getTagName()
        {
            List<Configuration> tags = new List<Configuration>();
            tags.Add(new Configuration( 1,"contractid", "123"));
            tags.Add(new Configuration(2,"SuperDuper", "332123"));
            tags.Add(new Configuration(22, "rama", "yyy"));
            tags.Add(new Configuration(32, "aktiv",  "123456"));
            tags.Add(new Configuration(42, "data ","xx764fhx"));
            return tags;

        }
        public static List<Configuration> getDocTags()
        {
            List<Configuration> docTags = new List<Configuration>();
            docTags.Add(new Configuration(1, 2));
            docTags.Add(new Configuration(1, 1));
            docTags.Add(new Configuration(1, 22));
            docTags.Add(new Configuration(2, 2));
            docTags.Add(new Configuration(2, 32));
            return docTags;
        }        
    }
}
4

4 回答 4

5

你想要一个Join

var commonUsers = (from t in tags
                  join d in docs on t.DocKey equals d.DocKey
                  select t)
                  .Distinct();  // to remove duplicates

或者另一种方式

var commonUsers = tags.Where(t => docs.Any(d => d.DocKey == t.DocKey));

第二种方法更短,但第一种可能会更好。

于 2013-08-29T20:39:48.817 回答
1
var newList = docs.Intersect(tags);

您可能需要覆盖 Equals 运算符才能使其正常工作。

于 2013-08-29T20:39:37.443 回答
1

下面是如何在不重载 Equals 方法的情况下使用Intersect执行此操作。您需要创建一个新类作为IEqualityComparer

class ConfigurationComparer : IEqualityComparer<Configuration>
{
    //You can change which string comparer fits your needs best.
    private readonly StringComparer comparer = StringComparer.CurrentCulture;

    public bool Equals(Configuration x, Configuration y)
    {
        return comparer.Equals(x.DocKey,y.DocKey);
    }

    public int GetHashCode(Configuration obj)
    {
         return comparer.GetHashCode(obj.DocKey);
    }

}

然后你只需做

var newList = tags.Intersect(docs, new ConfigurationComparer());
于 2013-08-29T20:49:44.433 回答
0

您可以使用Contains方法并将其应用于具有 lambda 表达式的过滤器,以使用一个或多个条件从另一个列表中提取项目,例如:

var result = needThis.Where(x => needThis2.Contains(x)).ToList();

或者使用扩展方法Intersect,例如:

var result = needThis.Intersect(needThis2);
于 2013-08-29T20:38:33.137 回答