简体   繁体   English

左外部将Linq连接到具有MVC模型的实体

[英]Left outer join Linq to Entities with MVC model

I have researched this and apologize if this is duplicate, but I cant seem to find the correct answer, and I learn so much from this site. 我已经对此进行了研究,并对是否重复感到抱歉,但是我似乎找不到正确的答案,并且我从该站点中学到了很多。

I have 3 tables: 我有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]

I now need to add a 4th table using a left outer join, to only get a default value of 0 for the VisitCount column if no records exist. 现在,我需要使用左外部联接添加第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]

Here is my latest attempt which returns the error "The entity or complex type 'DALModel.FavoriteClient' cannot be constructed in a LINQ to Entities query" but that's exactly what intellisense seems to be asking for (?) 这是我最近的尝试,返回错误“无法在LINQ to Entities查询中构造实体或复杂类型'DALModel.FavoriteClient'”,但这恰恰是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());

I am fairly new to Linq and while all the examples I have read make sense as explained, they fail miserably in implementation. 我对Linq相当陌生,尽管我阅读的所有示例都可以理解,但它们在实现上却惨败。 Please advise if you can and thanx in advance. 请告知您是否可以提前告知。

I just got it working: 我刚开始工作:

        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)
                };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM