gpt4 book ai didi

C# - SQL - LINQ 查询

转载 作者:太空宇宙 更新时间:2023-11-03 19:21:28 24 4
gpt4 key购买 nike

这个查询有一些问题::

var q2 =
from sd in db.ServerDrives
where sd.DriveLetter == driveList[i].Name
select sd;
ServerDrive existingServerDrives = q.First();
existingServerDrives.FreeSpace = driveList[i].FreeSpace;
//..
db.SubmitChanges();

我的问题是我不知道将 [i] 的引用放在哪里。

引用 [i]::

for (int i = 0; i < driveList.Count; i++)

我可以把它放在上面,

existingServerDrives.FreeSpace = driveList[i].FreeSpace;

并且该行可以识别它,但由于 .(下面的代码),我需要整个查询来识别它。行

where sd.DriveLetter == driveList[i].Name;

有谁知道我怎么可能做到这一点?

编辑:完整代码::

class Program
{
List<DriveInfo> driveList = DriveInfo.GetDrives().Where(x => x.IsReady).ToList<DriveInfo>(); //Get all the drive info
Server server = new Server(); //Create the server object
ServerDrive serverDrives = new ServerDrive();

public static void Main()
{
Program c = new Program();
c.RealDriveInfo();
c.WriteInToDB();
}

public void RealDriveInfo()
{


//Insert information of one server - You will need get information of all servers
server.ServerID = 0; //Here is necessery put PK key. I recommend doing the SQL server will automatically generate the PK.
server.ServerName = string.Concat(System.Environment.MachineName);

//Inserts information in the newServers object
for (int i = 0; i < driveList.Count; i++)
{

//Put here all the information to object Server
serverDrives.DriveLetter = driveList[i].Name;
serverDrives.TotalSpace = driveList[i].TotalSize;
serverDrives.DriveLabel = driveList[i].VolumeLabel;
serverDrives.FreeSpace = driveList[i].TotalFreeSpace;
serverDrives.DriveType = driveList[i].DriveFormat;
server.ServerDrives.Add(serverDrives);

}
}

public void WriteInToDB()
{
//Add the information to an SQL Database using Linq.
DataClasses1DataContext db = new DataClasses1DataContext(@"sqlserver");
db.Servers.InsertOnSubmit(server);

var q2 =
from s in db.Servers
where s.ServerName == "LAPTOP-043"
select s;
Server existingServers = q2.First();
for (int i = 0; i < driveList.Count; i++)
existingServers.ServerName = string.Concat(System.Environment.MachineName);
//..
for (int i = 0; i < driveList.Count; i++)
{
var q =
from sd in db.ServerDrives
where sd.DriveLetter == driveList[i].Name
select sd;
ServerDrive existingServerDrives = q.First();
existingServerDrives.FreeSpace = driveList[i].FreeSpace;
//..
db.SubmitChanges();

我想要做的是让查询找到与这台计算机上的驱动器 C:\相同的“DriveLetter”行。第二部分应该更改 FreeSpace 值并将其替换为从控制台应用程序检索到的新 FreeSpace 值。

如有任何反馈,我们将不胜感激,提前致谢:)

最佳答案

不确定你想说什么或做什么,但是这个怎么样:

for (int i =0; i < driveList.Count; i++)
{
var q2 =
from sd in db.ServerDrives
where sd.DriveLetter == driveList[i].Name
select sd;
ServerDrive existingServerDrives = q2.First();
existingServerDrives.FreeSpace = driveList[i].FreeSpace;
//..
db.SubmitChanges();
}

关于C# - SQL - LINQ 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11898957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com