我研究了这个,如果这是重复的话道歉,但我似乎无法找到正确的答案,而且我从这个网站上学到了很多。
我有3张桌子:
CREATE TABLE [Reporting].[ReportingCompanies](
[ID] [int] IDENTITY(1,1) NOT NULL,
[EINC] [int] NOT NULL,
[CompanyId] [varchar](4) NOT NULL,
[CompanyName] [varchar](50) NOT NULL,
[LastClosedWeek] [datetime] NOT NULL,
[PriorWeekSales] [decimal](14, 4) NULL,
[PriorWeek] [datetime] NULL,
[CurrentWeek] [datetime] NULL,
[LastWeekSales] [decimal](14, 4) NULL,
[AgingBalance] [decimal](14, 4) NULL,
[DataAsOf] [datetime] NULL,
[Tier] [int] NULL,
[CurrentWeeklyAverage] [decimal](14, 4) NULL,
[AvgAged] [decimal](14, 4) NULL,
[hasSpread] [bit] NULL,
[DSO] [int] NULL,
[isFamily] [bit] NULL,
[ImportStatus] [bit] NULL,
[ClientStatus] [varchar](50) NULL,
CONSTRAINT [PK_ReportingCompanies] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [Reporting].[AllowedCompanies](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CustomUserProfileUserId] [uniqueidentifier] NOT NULL,
[CustomUserProfileID] [int] NOT NULL,
[ReportingCompanyID] [int] NOT NULL,
CONSTRAINT [PK_AllowedCompanies] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [Reporting].[CustomUserProfiles](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ApplicationId] [uniqueidentifier] NOT NULL,
[UserId] [uniqueidentifier] NOT NULL,
[UserName] [nvarchar](256) NOT NULL,
[LoweredUserName] [nvarchar](256) NOT NULL,
[MobileAlias] [nvarchar](16) NULL,
[IsAnonymous] [bit] NOT NULL,
[LastActivityDate] [datetime] NOT NULL,
[ImportStatus] [bit] NULL,
CONSTRAINT [PK_CustomUserProfiles] PRIMARY KEY CLUSTERED
(
[UserId] ASC,
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
我现在需要使用左外连接添加第4个表,如果没有记录,则只获取VisitCount列的默认值0。
CREATE TABLE [PayJot].[FavoriteClients](
[FavRowID] [int] IDENTITY(1,1) NOT NULL,
[Userid] [uniqueidentifier] NOT NULL,
[CompanyId] [int] NOT NULL,
[VisitCount] [int] NOT NULL,
CONSTRAINT [PK_FavoriteClients] PRIMARY KEY CLUSTERED
(
[FavRowID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
这是我最近的尝试,它返回错误"实体或复杂类型' DALModel.FavoriteClient'不能在LINQ to Entities查询中构造"但这正是intellisense似乎要求的(?)
var userName = HttpContext.Current.User.Identity.Name;
var q = from c in db.ReportingCompanies1
join a in db.AllowedCompanies on new { ID = (int)c.ID } equals new { ID = a.ReportingCompanyID }
join p in db.CustomUserProfiles on new { CustomUserProfileID = a.CustomUserProfileID } equals new { CustomUserProfileID = p.ID }
join f in db.FavoriteClients on new { A = c.ID, B = p.UserId } equals new { A = f.CompanyId, B = f.Userid } into fvc
from userfavs in fvc.DefaultIfEmpty(new FavoriteClient { CompanyId = c.ID, Userid = p.UserId , VisitCount = 0 })
where
p.UserName == userName
orderby c.CompanyName
select new Clients()
{
ID = c.ID,
CompanyId = c.CompanyId,
CompanyName = c.CompanyName,
visitCount = userfavs.VisitCount
};
ObservableCollection<Clients> clientList = new ObservableCollection<Clients>(q.ToList());
我对Linq来说相当新,虽然我所阅读的所有例子都有所解释,但是他们在实施中却失败了。请提前告知您是否可以和他们。
答案 0 :(得分:1)
我刚刚开始工作:
var q = from p in db.CustomUserProfiles
join a in db.AllowedCompanies on new { ID = p.ID } equals new { ID = a.CustomUserProfileID }
join c in db.ReportingCompanies1 on new { ReportingCompanyID = (int)(int)a.ReportingCompanyID } equals new { ReportingCompanyID = c.ID }
join f in db.FavoriteClients
on new { a.CustomUserProfileUserId, ReportingCompanyID = a.ReportingCompanyID }
equals new { CustomUserProfileUserId = (Guid)f.Userid, ReportingCompanyID = f.CompanyId } into FavoriteClients_join
from FavoriteClients in FavoriteClients_join.DefaultIfEmpty()
where
p.UserName == userName
orderby
FavoriteClients.VisitCount descending,
c.CompanyName
select new Clients()
{
ID = c.ID,
CompanyId = c.CompanyId,
CompanyName = c.CompanyName,
visitCount = ((Int32?)FavoriteClients.VisitCount ?? (Int32)0)
};