使用LINQ和C优化Union、Except、Join的查询

使用LINQ和C优化Union、Except、Join的查询,linq,join,union,except,Linq,Join,Union,Except,我有两个从XML报告和数据库加载的对象列表,代码如下所示,我应该对它们进行分析,并根据一些条件将项目标记为0、1、2、3 TransactionResultCode = 0; // SUCCESS (all fields are equivalents: [Id, AccountNumber, Date, Amount]) TransactionResultCode = 1; // Exists in report but Not in database TransactionResultCo

我有两个从XML报告和数据库加载的对象列表,代码如下所示,我应该对它们进行分析,并根据一些条件将项目标记为0、1、2、3

TransactionResultCode = 0; // SUCCESS (all fields are equivalents: [Id, AccountNumber, Date, Amount])
TransactionResultCode = 1; // Exists in report but Not in database 
TransactionResultCode = 2; // Exists in database but Not in report 
TransactionResultCode = 3; // Field [Id] are equals but other fields [AccountNumber, Date, Amount] are different.
如果有人能抽出时间来建议如何优化一些查询,我会很高兴的。 下面是代码:

谢谢你


您可以尝试以下方法:

public void Test()
{
    var report = new[] {new Item(1, "foo", "boo"), new Item(2, "foo2", "boo2"), new Item(3, "foo3", "boo3")};
    var dataBase = new[] {new Item(1, "foo", "boo"), new Item(2, "foo22", "boo2"), new Item(4, "txt", "rt")};

    Func<Item, bool> inBothLists = (i) => report.Contains(i) && dataBase.Contains(i);
    Func<IEnumerable<Item>, Item, bool> containsWithID = (e, i) => e.Select(_ => _.ID).Contains(i.ID);

    Func<Item, int> getCode = i =>
                                    {
                                        if (inBothLists(i))
                                        {
                                            return 0;
                                        }
                                        if(containsWithID(report, i) && containsWithID(dataBase, i))
                                        {
                                            return 3;
                                        }
                                        if (report.Contains(i))
                                        {
                                            return 2;
                                        }
                                        else return 1;
                                    };

    var result = (from item in dataBase.Union(report) select new {Code = getCode(item), Item = item}).Distinct();
}

public class Item
{
    // You need also to override Equals() and GetHashCode().. I omitted them to save space
    public Item(int id, string text1, string text2)
    {
        ID = id;
        Text1 = text1;
        Text2 = text2;
    }

    public int ID { get; set; }
    public string Text1 { get; set; }
    public string Text2 { get; set; }
}

请注意,您需要为您的项实现Equals,或者实现IEqualityComparer并将其提供给Contains方法

其他人能看到这个问题吗?
public void Test()
{
    var report = new[] {new Item(1, "foo", "boo"), new Item(2, "foo2", "boo2"), new Item(3, "foo3", "boo3")};
    var dataBase = new[] {new Item(1, "foo", "boo"), new Item(2, "foo22", "boo2"), new Item(4, "txt", "rt")};

    Func<Item, bool> inBothLists = (i) => report.Contains(i) && dataBase.Contains(i);
    Func<IEnumerable<Item>, Item, bool> containsWithID = (e, i) => e.Select(_ => _.ID).Contains(i.ID);

    Func<Item, int> getCode = i =>
                                    {
                                        if (inBothLists(i))
                                        {
                                            return 0;
                                        }
                                        if(containsWithID(report, i) && containsWithID(dataBase, i))
                                        {
                                            return 3;
                                        }
                                        if (report.Contains(i))
                                        {
                                            return 2;
                                        }
                                        else return 1;
                                    };

    var result = (from item in dataBase.Union(report) select new {Code = getCode(item), Item = item}).Distinct();
}

public class Item
{
    // You need also to override Equals() and GetHashCode().. I omitted them to save space
    public Item(int id, string text1, string text2)
    {
        ID = id;
        Text1 = text1;
        Text2 = text2;
    }

    public int ID { get; set; }
    public string Text1 { get; set; }
    public string Text2 { get; set; }
}