# 使用dapper.net内部联接的spliton问题

dapper sql

### 题

``````  var resultList = sqlCon.Query<UserProfile, UserAddress, UserProfile>(@"

UPDATE [User].[User_Profile]
SET ProfileStatus = 4
WHERE Id = @UserId

u.ProfileStatus,
a.Latitude, a.Longitude
FROM [User].[User_Profile] u
INNER JOIN [User].[User_Address] a on u.id = a.UserId
WHERE u.Id = @UserId", (u, a) =>
{
u.Id = a.UserId;
return u;
},
new { UserId = userId }, splitOn: "UserId").FirstOrDefault();
``````

`````` public class UserProfile
{
public Int64 UserId { get; set; }

public string UserName { get; set; }

public int Age { get; set; }

public int ProfileStatus { get; set; }

public Int64 AddressId { get; set; }

public int Country { get; set; }

public int State { get; set; }

public int City { get; set; }

public decimal Latitude { get; set; }

public decimal Longitude { get; set; }
}
``````

`````` public class UserAddress
{
public Int64 Id { get; set; }

public Int64 UserId { get; set; }

public int Country { get; set; }

public int State { get; set; }

public int Town { get; set; }

public string PostCode { get; set; }

public decimal Latitude { get; set; }

public decimal Longitude { get; set; }
}
``````

**更新**

``````                            SELECT u.Id as [UserId], u.Username as [Username], u.Age as [Age],
u.ProfileStatus as [ProfileStatus],
a.Id as [AddressId], a.Country as [Country], a.[State] as [State],
a.City as [City], a.Latitude as [Latitude], a.Longitude as [Longitude]
FROM [User].[User_Profile] u
INNER JOIN [User].[User_Address] a on u.id = a.UserId
WHERE u.Id = @UserId", (u, a) =>
{
u.UserId = a.UserId;
return u;
},
new { UserId = userId }, splitOn: "AddressId").FirstOrDefault();
``````

### 热门答案

`SplitOn`不正确，因为`UserId`是查询中的第一列。您必须提供从第二个表中`User_Profile`第一个表/类的列，因此第一个列/别名不属于表`User_Profile`但属于`User_Address`

``````// ...
new { UserId = userId }, splitOn: "AddressId").FirstOrDefault();
``````

Prime Library

More Projects...