我在LINQ TO SQL中这样做:
var query = Database.ExtendedUsers.Select(x => x).Where(x => x.Acolumn >= 4);
Run Code Online (Sandbox Code Playgroud)
它生成正确的SQL语法(使用where子句).如果我添加第二个,则忽略它:
var query = Database.ExtendedUsers.Select(x => x).Where(x => x.Acolumn >= 4);
query.Where(x => x.AnotherColumn.Equals(2));
Run Code Online (Sandbox Code Playgroud)
第二个where子句未添加到SQL查询中. 我究竟做错了什么?
基本上,我想动态添加的地方
query.Where(...);
query.Where(...);
query.Where(...);
query.ToList(); /: result
Run Code Online (Sandbox Code Playgroud) 如何使用带有匿名类型的select new关键字将linq更新为sql值,因为我使用了var关键字并选择了我需要的新查询但是它返回了这样的错误
编译器错误消息:CS0200:属性或索引器'AnonymousType#1.Code'无法分配 - 它是只读的
这是我的代码:
var ProjectView12 = (from x in db.Projects
select new
{
add = db.Locations.Where(y = > y.ID == x.RegionID).FirstOrDefault().Name,
Province = db.Locations.Where(y = > y.ID == x.ProvinceID).FirstOrDefault().Name,
District = db.Locations.Where(y = > y.ID == x.DistrictID).FirstOrDefault().Name,
Code = x.Code,
Name = x.Name,
ProjectIdentificationDate = db.Milestones.Where(y = > y.ProjectID == x.ID && y.StageID == 1 && y.ComponentID == 1 && y.ModuleID == 1).FirstOrDefault().Date.ToString(),
ProjectLat = Convert.ToDecimal(x.Lat),
ProjectLong = Convert.ToDecimal(x.Lon),
Remarks = db.Milestones.Where(y = > y.ProjectID …Run Code Online (Sandbox Code Playgroud) 我的数据库中有600万条记录,我想加载所有这个查询:
var query=(from p in behzad.test
select p).ToArray();
Run Code Online (Sandbox Code Playgroud)
我可以加载吗?
如您所知,LINQ to SQL生成自己的实体.我尝试从数据库中获取数据并将其放在我自己的实体中,我创建的类.
当我这样做时:
AppData.MyDBDataContext context = new AppData.MyDBDataContext();
List<User> users = (from user in context.Users.Select(x => new User
{
Id = x.Id,
Name = x.Name,
Password = x.Password
}) where user.Password == "123456" select user).ToList();
Run Code Online (Sandbox Code Playgroud)
它有效(User是我创建的一个类).但是当我尝试概括和构建一个强制转换函数时:
User castToUser (DataAccess.AppData.User linqEntity)
{
return new User
{
Id = linqEntity.Id,
Name = linqEntity.Name,
Password = linqEntity.Password
};
}
Run Code Online (Sandbox Code Playgroud)
(DataAccess.AppData.User是LINQ to SQL生成的实体).
AppData.MyDBDataContext context = new AppData.MyDBDataContext();
List<User> users = (from user in context.Users.Select(x => castToUser(x)) where user.Password == "123456" …Run Code Online (Sandbox Code Playgroud) ID UserName BookType bookID
1 Krish 1 1
1 Krish 1 2
1 Krish 2 1
1 Krish 2 2
2 Ram 1 1
3 Raj 1 1
3 Raj 1 2
Run Code Online (Sandbox Code Playgroud)
我有上面的表,我想获得每个用户的独特的booktype计数和bokkID计数,我能够在sqlserver中写这个,但是当我来LINQ无法编写查询时.
我需要以下输出,
ID UserName BookType bookID
1 Krish 2 2
2 Ram 1 1
3 Raj 1 2
Run Code Online (Sandbox Code Playgroud) 我想获得Patient的Firstname从数据库中,但什么我得到当我运行这段代码仅仅是一个LINQ表达式字符串,而不是.
public static string GetPatientName(int bedNumber)
{
var name = "";
using (var data = new HospitalDBDataContext())
{
if (data.Connection.State == ConnectionState.Closed)
data.Connection.Open();
name = data.Patients.Where(x => x.BedNumber.Equals(bedNumber))
.Select(x => x.Firstname).ToString();
if (data.Connection.State == ConnectionState.Open)
data.Connection.Close();
data.Dispose();
}
return name;
}
Run Code Online (Sandbox Code Playgroud)
使这个表达式返回我需要的实际值的方法是什么?
我有一个看起来像这样的查询:
var TheDataToDelete = (from x in MyDC.SomeTable
where x.....
select x).ToList();
if (TheDataToDelete.Count > 0)
{
MyDC.SomeTable.DeleteAllOnSubmit(TheDataToDelete);
MyDC.SubmitChanges();
}
Run Code Online (Sandbox Code Playgroud)
有大约10K行要删除,在我的错误日志中,我有时(每周一次)看到此错误:
Inner Exception Type: System.ComponentModel.Win32Exception
Inner Exception: The wait operation timed out
Inner Source:
Exception Type: System.Data.SqlClient.SqlException
Exception: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题,我需要修改什么来修复它?
我有这两个代码,我必须知道哪个更好用.我在想这是一样的.如果我没有弄错,第一个只有一次调用数据库,但是,我不知道检查repo!= null是否正确.
(1)
var repo = Repository
.Query()
.Where(ur => ur.CustomerId == customerId)
.SingleOrDefault();
if (repo != null)
{
// Update repo
repo.Name = "name here";
}
else
{
// code
}
Run Code Online (Sandbox Code Playgroud)
(2)
var repo = Repository
.Query()
.Any(ur => ur.CustomerId == customerId);
if (repo)
{
var result = Repository
.Query()
.Where(ur => ur.CustomerId == customerId)
.Single();
result.Name = "name here";
}
else
{
// code
}
Run Code Online (Sandbox Code Playgroud) 我有国家列表,里面有地方列表
...
public IList<ICountriesDTO> Countries { get; set; }
public class CountriesDTO: ICountriesDTO
{
public IEnumerable<IPlacesDTO> Places { get; set;
}
Run Code Online (Sandbox Code Playgroud)
我想获取地方列表如果没有 null
allPlacesDTO.World.Countries.SelectMany(x => x.Places == null ? null : x.Places).ToList();
Run Code Online (Sandbox Code Playgroud)
但它给出的null exception地方是null为国家object.
我如何null检查Places并只使用return语句而不是选择null object,如下所示?
if (allPlacesDTO.World.Countries.Places == null)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
更新:
我的要求是,如果在任何一个国家没有地方只使用return声明退出当前功能而不继续进行.这是通过公认的答案和Count功能实现的.
var lstAllPlaces = allPlacesDTO.World.Countries.Where(x => x.Places != null).SelectMany(x => x.Places).ToList();
if (lstAllPlaces.Count() == 0)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家的答案.欣赏.
不知道为什么我在下面运行此查询时遇到此错误
无法创建"YogaBandy2017.Models.Profile.YogaProfile"类型的常量值.在此上下文中仅支持基元类型或枚举类型.
var requestedEvents = dbContext.YogaSpaceEvents
.Where(i => i.RequestedInstructor == yogaProfile)
.OrderByDescending(i => i.EventDateTime)
.Select(i => new PendingEvent
{
SpaceName = i.YogaSpace.Overview.Title,
SpaceImage = i.YogaSpace.YogaSpaceImages
.Where(j => j.IsMainImage)
.Select(j => j.ImageThumbnailCropped11)
.FirstOrDefault(),
SpaceId = i.YogaSpace.YogaSpaceId,
SpaceEventsHosted = i.YogaSpace.ClassesHosted,
SpaceReviewNumber = i.YogaSpace.ReviewNumber,
SpaceReviewPercent = i.YogaSpace.ReviewPercentage,
SpaceNumberOfReviews = i.YogaSpace.NumberOfReviews,
HostImage = i.YogaSpace.YogaProfile.YogaProfileImages
.Where(k => k.IsMainImage)
.Select(k => k.ImageThumbnailCropped)
.FirstOrDefault(),
HostId = i.YogaSpace.YogaProfile.YogaProfileId,
HostName = i.YogaSpace.YogaProfile.FirstName,
EventDateTime = i.EventDateTime,
Style = i.StyleMain.ToString(),
Duration = i.Duration,
EventId = i.YogaSpaceEventId
})
.ToList();
Run Code Online (Sandbox Code Playgroud)