使用 LINQ 和 EF 4.1 填充字典具有相关实体数据的属性

发布于 2025-01-08 12:51:54 字数 532 浏览 0 评论 0原文

我有一个表Customers,它链接到另一个表Addresses。 地址表的主键由 {CustomerID, LanguageID} 组成。

我想编写一个 LINQ 查询,在其中实例化一个类型并填充其

Dictionary地址 {get;set;}

属性与地址表中的地址。每种语言都将成为字典中的关键。

就像这样:

from c in customers
from a in c.Addresses
select new DataContainer
{
  ID = c.CustomerId,
  ...
  Addresses.Add(a.LanguageId, a),
  ...
};

我知道我确实无法在对象初始值设定项中执行 .Add() 调用,但是有没有办法使其工作?

注意:我当然可以像往常一样创建类型,然后返回并显式填充 Addresses 属性。

I have a table Customers that is linked to another table Addresses.
The Addresses table has a Primary Key made up of {CustomerID, LanguageID}.

I would like to wrtie a LINQ query where I instantiate a type and populate its

Dictionary<string, Address> Addresses {get;set;}

property with the addresses in the Address table. Each Langauge is to become the key in the dictionary.

Like so:

from c in customers
from a in c.Addresses
select new DataContainer
{
  ID = c.CustomerId,
  ...
  Addresses.Add(a.LanguageId, a),
  ...
};

I know that I really can't do the .Add() call in an object initializer, but is there a way to make that work?

Note: I could of course create the type as usual and then go back in and populate the Addresses property explicitely.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林散布 2025-01-15 12:51:54

我不记得以下代码是否可以编译,但您可以尝试这样的事情:

from c in customers
from a in c.Addresses
select new DataContainer
{    
  Addresses = new Dictionary<string, Address> {{a.LanguageId, a}};
};

或者您可以尝试以下解决方案:
从LINQ 投影

I don't remember from memory if following code will compile, but you can try something like this:

from c in customers
from a in c.Addresses
select new DataContainer
{    
  Addresses = new Dictionary<string, Address> {{a.LanguageId, a}};
};

Or you can try following solution:
Instantiating a Dictionary from a LINQ projection

债姬 2025-01-15 12:51:54

一种安全的方法是这样的:

如果 DataContainer 看起来像这样:

public class DataContainer
{
    public string ID { get; set; }
    public Address Address { get; set; }
}

您可以这样做:

from c in customers
from a in c.Addresses
select new DataContainer
{
  ID = c.CustomerId,
  Address = a
};
var dic = new Dictionary<string, Address>();

foreach (var n in query)
{
  dic.Add(ID,a);
}

或者简单地说,这样做:

var dic = query.ToDictionary<DataContainer, string, Address>(n => n.ID, n => n.a);

A safe way to do this is like so:

If DataContainer looks like this:

public class DataContainer
{
    public string ID { get; set; }
    public Address Address { get; set; }
}

You can do this:

from c in customers
from a in c.Addresses
select new DataContainer
{
  ID = c.CustomerId,
  Address = a
};
var dic = new Dictionary<string, Address>();

foreach (var n in query)
{
  dic.Add(ID,a);
}

Or for short do this:

var dic = query.ToDictionary<DataContainer, string, Address>(n => n.ID, n => n.a);
机场等船 2025-01-15 12:51:54

回应约翰对亨克的评论。

我写了一个字典扩展

public static Dictionary<T, K> Build<T, K>(this Dictionary<T, K> dictionary, T key, K value)
{
       dictionary[key] = value;
       return dictionary;
}

现在你可以做这样的事情:

from c in customers
from a in c.Addresses
select new DataContainer
{    
  Addresses = new Dictionary<string, Address>()
                                              .Build(a.LanguageId, a)
};

对我来说它确实有效。 :)

In response to John's comment for henk.

I wrote a Dictionary extension

public static Dictionary<T, K> Build<T, K>(this Dictionary<T, K> dictionary, T key, K value)
{
       dictionary[key] = value;
       return dictionary;
}

Now you can do something like this:

from c in customers
from a in c.Addresses
select new DataContainer
{    
  Addresses = new Dictionary<string, Address>()
                                              .Build(a.LanguageId, a)
};

For me it does work. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文