[英]Need help populating an asp:dropdownlist from multiple tables in an SQL database
I have 4 tables: CustomerDocument, CustomerLink, CustomerAdditionalInfo, and CustomerImage. 我有4个表:CustomerDocument,CustomerLink,CustomerAdditionalInfo和CustomerImage。 They each have a value of CustomerNumber, and I need to way to pull the customer numbers from each table and put in a drop down list.
他们每个人都有一个CustomerNumber值,我需要设法从每个表中提取客户编号并放入一个下拉列表中。 I know how to do it with one table, but not multiple.
我知道如何用一张桌子,而不是多个桌子。 Also, there is a restriction that the CustomerNumber needs to be not null, so do I need to include this with each join?
此外,还有一个限制,即CustomerNumber必须不为null,因此是否需要在每个联接中都包括此值? Here is a bit of code I have now.
这是我现在拥有的一些代码。 oDb is the DataContext
oDb是DataContext
var oData = from c in oDb.CustomerAdditionalInfos
where ( c.CustomerID == CustomerID &&
c.CustomerNumber != null &&
c.CategoryID == CategoryID )
orderby c.CustomerNumber
select new { c.CustomerNumber };
return oData;
You could do this.... 你可以这样做...
var oData = (from c in oDb.CustomerAdditionalInfos
where c.CustomerNumber != null
select new
{
CustomerNumber = c.CustomerNumber
}).Union
(from d in oDb.CustomerDocument
where d.CustomerNumber != null
select new
{
CustomerNumber = d.CustomerNumber
}).Union
(from l in oDb.CustomerLink
where l.CustomerNumber != null
select new
{
CustomerNumber = l.CustomerNumber
}).Union
(from i in oDb.CustomerImage
where i.CustomerNumber != null
select new
{
CustomerNumber = i.CustomerNumber
}).OrderBy(c => c.CustomerNumber);
That is simply a union of all of the CustomerNumbers in all four tables. 那只是所有四个表中所有CustomerNumbers的并集。 This WILL include duplicates if there are duplicates.
如果有重复项,它将包括重复项。 If you want only distinct CustomerNumbers, then just do a
Distinct()
after the OrderBy
. 如果只需要不同的CustomerNumber,则只需在
OrderBy
之后执行Distinct()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.