c# - LINQ 查询匹配两个项目列表

标签 c# asp.net linq entity-framework

我的问题是我有一个来自 Entity Framework 的“属性”属性。 因此,我检索这个具有属性标签列表的对象,它们可以通过 attribute.AttributeTags 访问。 。现在我有一个asp:TextBox用户可以在其中编辑、删除和添加新标签(以逗号分隔)。 (在页面加载时,我将属性标签添加到此 TextBox )

在页面上回发后,我返回用户输入并将其拆分为字符串数组并将其存储在名为 AttributeTags 的变量中.

现在,我想添加原始 attributes 中未包含的新标签。来自 EF 的列表,想要 remove attributes 中包含的内容但在用户输入字符串数组中找不到 AttributeTags .

我正在做这样的事情:

        BusinessObjects.Attribute attribute = db.Attributes.FirstOrDefault(a => a.attribute_id == AttributeID);
        string[] AttributeTags = txtAttributeTags.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        foreach (var item in AttributeTags)
        {
            if (!attribute.AttributeTags.Any(t => t.value == item))
            {
                AttributeTag tag = new AttributeTag { value = item, timestamp = DateTime.Now };
                attribute.AttributeTags.Add(tag);
            }
            else
            {
                AttributeTag tag = attribute.AttributeTags.FirstOrDefault(t => t.value == item);
            }
        }

但是我有点被困在这里,因为我对 LINQ 和 EF 还很陌生。

最佳答案

对于这种情况我有两种解决方案。

<小时/>

第一个解决方案

我们可以创建一个ExcepWith方法允许我们删除 ICollection<T> 中的所有项目已经存在的给出 IEnumerable<T> 。该方法的代码如下:

public static int ExceptWith<TItem>
(
    this ICollection<TItem> collection,
    IEnumerable<TItem> other
)
{
    if (ReferenceEquals(collection, null))
    {
        throw new ArgumentNullException("collection");
    }
    else if (ReferenceEquals(other, null))
    {
        throw new ArgumentNullException("other");
    }
    else
    {
        int count = 0;
        foreach (var item in other)
        {
            while (collection.Remove(item))
            {
                count++;
            }
        }
        return count;
    }
}

现在你有一个string[]根据用户的输入,该数组是 IEnumerable<string>但不是 ICollection<string> ...这很容易解决,如下所示:

而不是这个:

string[] AttributeTags =
    txtAttributeTags.Text.Split
    (
        new string[] { "," },
        StringSplitOptions.RemoveEmptyEntries
    );

你这样做:

var AttributeTags =
    new List<string>
    (
        txtAttributeTags.Text.Split
        (
            new string[] { "," },
            StringSplitOptions.RemoveEmptyEntries
        )
    );

甚至是这样:

var AttributeTags =
    txtAttributeTags.Text.Split
    (
        new string[] { "," },
        StringSplitOptions.RemoveEmptyEntries
    ).ToList();

现在你可以这样做:

AttriuteTags.ExceptWith(existingTags);

由于 attribute.AttributeTag 的类型不是 IEnumerable<string>您使用选择:

AttriuteTags.ExceptWith(attribute.AttributeTag.Select(item => item.value));

这样列表中就只剩下新标签了。

<小时/>

注意:这个方法依赖于Remove的实现,如果你需要做特殊的比较,那么你就不走运了。

<小时/>

第二种解决方案

还有另一种方法。您可以使用 Except来自Enumerable类。

string[] AttributeTags =
    txtAttributeTags.Text.Split
    (
        new string[] { "," },
        StringSplitOptions.RemoveEmptyEntries
    );
var newTags = AttributeTags.Except(existingTags);

由于 attribute.AttributeTag 的类型不是 IEnumerable<string>您使用选择:

string[] AttributeTags =
    txtAttributeTags.Text.Split
    (
        new string[] { "," },
        StringSplitOptions.RemoveEmptyEntries
    );
var newTags = AttributeTags.Except
(
    attribute.AttributeTag.Select(item => item.value)
);

这会放入 newTags,嗯,新标签。

<小时/>

注意:如果您需要进行特殊比较,那么您应该使用 the other overload of the method :

string[] AttributeTags =
    txtAttributeTags.Text.Split
    (
        new string[] { "," },
        StringSplitOptions.RemoveEmptyEntries
    );
var newTags = AttributeTags.Except(attribute.AttributeTag, equalityComparer);

遗憾的是,equalityComparer 是实现 IEqualityComparer 的类的对象,这意味着您不能在那里使用 lambda。为此,您可以添加此类:

public class CustomEqualityComparer<T> : IEqualityComparer<T>
{
    private Func<T, T, bool> _comparison;
    private Func<T, int> _getHashCode;

    public CustomEqualityComparer
    (
        Func<T, T, bool> comparison,
        Func<T, int> getHashCode
    )
    {
        if (ReferenceEquals(comparison, null))
        {
            throw new ArgumentNullException("comparison");
        }
        else if (ReferenceEquals(getHashCode, null))
        {
            throw new ArgumentNullException("getHashCode");
        }
        else
        {
           _comparison = comparison;
           _getHashCode = getHashCode;
        }
    }

    public bool Equals(T x, T y)
    {
        return _comparison.Invoke(x, y);
    }

    public int GetHashCode(T obj)
    {
        return _getHashCode.Invoke(obj);
    }
}

现在像这样调用(例如):

string[] AttributeTags =
    txtAttributeTags.Text.Split
    (
        new string[] { "," },
        StringSplitOptions.RemoveEmptyEntries
    );
var newTags = AttributeTags.Except
(
    existingTags,
    new CustomEqualityComparer<string>
    (
        (a, b) => 1, //your custom comparison here
        str => str.GetHashCode()
    )
);

由于 attribute.AttributeTag 的类型不是 IEnumerable<string>您使用选择:

string[] AttributeTags =
    txtAttributeTags.Text.Split
    (
        new string[] { "," },
        StringSplitOptions.RemoveEmptyEntries
    );
var newTags = AttributeTags.Except
(
    attribute.AttributeTag.Select(item => item.value),
    new CustomEqualityComparer<string>
    (
        (a, b) => 1, //your custom comparison here
        str => str.GetHashCode()
    )
);
<小时/>

添加新标签

现在您已经有了新标签,让我们说 newTags ,您可以迭代它以添加新标签:

var now = DateTime.Now;
foreach (var item in newTags)
{
    AttributeTag tag = new AttributeTag { value = item, timestamp = now };
    attribute.AttributeTags.Add(tag);
}
<小时/>

比较解决方案

这些方法有什么区别?

  • 第一个需要较少的内存
  • 第一个需要定义一个新方法。
  • 第一个不允许自定义 IEqualityComparer<T>
  • 第二个允许延迟执行。
  • 第二个使用(不需要)辅助类。

关于c# - LINQ 查询匹配两个项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12767721/

相关文章:

html - ASP.Net MVC 4 在提交表单时设置 'onsubmit'

c# - 将 sql 查询转换为 Entity Framework

c# - 用于查找给定时间间隔内所有星期日和星期五的优化算法

c# - 动态 double 的 Linq 平均值

c# - 如何使用正则表达式将字符串 `{{key.value}}` 拆分为两个字符串 `key` 和 `value` ?

c# - VS 项目设置正在构建一个没有 <appSettings> 或 <add> 元素的 .config 文件

c# - 获取名称以特定字符串开头的所有控件

c# - 从 Internet/Intranet 访问 SQL Server

c# - Xml反序列化器不工作

asp.net - AJAX - TagPrefix 未注册