35

使用 LINQ 从我的数据库查询 IQueryable 结果,如何将新记录添加到 IQueryable 结果。

4

8 回答 8

42

您要将其添加到数据库中,还是仅添加到数据绑定等其他事物将使用的结果列表中?

如果是前者,则需要对正在使用的任何类型的 LINQ 使用正常的添加操作,但对表表示而不是 IQueryable。查询只读取数据。

如果是后者,请使用Enumerable.Concat将现有序列(IQueryable)与包含额外条目的新序列(在这里可以使用数组或列表)连接起来,并将结果用于绑定等。这可能值得首先使用 AsEnumerable() 以确保使用 Enumerable 而不是 Queryable。例如:

IQueryable<string> names = [...];
names = names.AsEnumerable().Concat(new[] { "(None)", "(Add new)" });
于 2009-01-12T07:58:04.410 回答
16

您应该首先将其转换为列表;

IQueryable<int> foo = new SomeQueryable<int<();
List<int> list = foo.ToList();
list.Add(5);

或使用 Concat

IQueryable<int> foo = new SomeQueryable<int<();
foo = foo.Concat(new int[]{5});

编辑:确定你必须重新分配 foo.

于 2009-01-12T07:55:53.963 回答
6

The simple answer is that unless you add the record to the underlying datastore that the Iqueryable is querying, you can't add a new record into an IQueryable. So if you are using LinqToSql then you would have to add a row into the table that the IQueryable was querying in order to "add" a row into the IQueryable.

Remember that an IQueryable is not a result set, it is a query. Until you use something like .ToList() the IQueryable will not evaluate a result set and more importantly, the IQueryable doesn't hang on to that result set. It creates a list and puts the results into in instead. So that means that if you call ToList() on an Iqueryable twice, it will go off and query the database twice. It doesn't care that it might be inefficient.

So, if you look at the examples that others have used above, a simple change of AsEnumerable() to ToList() will most likely fix your problems.

Something like:

dcDataContext db = new dcDataContext();
var query = from c in db.tblSexes
              select new { c.SexCode, c.SexName };

var results = query.ToList().Concat(new[] { new { SexCode = -1, SexName = "Please select your Gender" } });

//or even better now that it's a list
var results = query.ToList().Add(new { SexCode = -1, SexName = "Please select your Gender" });

SelectList sliSex = new SelectList(results, "SexCode", "SexName");
于 2009-02-18T03:21:30.320 回答
3

这可能是一个非常古老的问题,我想在这里添加更多解释和示例

如果使用IQueryable<YourObject>,则必须将其转换为IEnumerable<YourObject>first

有关 IEnumerable 的附加详细信息:

返回 IEnumerable<T> 与 IQueryable<T>

经过

IQueryable<YourObject> iqueryResult = // ..... your LinQ or whatever
IEnumerable<YourObject> enumResult = iqueryResult.AsEnumerable();

然后,添加你可以通过

enumResult = enumResult.Concat(new[] {new YourObject()
                 { 
                    //.... 
                 } 
             });

真实代码示例

var iquery_QRDTR = from rrp in P_QCDTR
        select new WebRequestData
        {
             ros_chk_id = rrp.CHECKIN_ID,
             ros_img = rrp.EMPLOYEE_ASSOCIATE.IMAGE_URL
        };

var enum_QRDTR = iquery_QRDTR.AsEnumerable();

enum_QRDTR = enum_QRDTR.Concat(new[] {new WebRequestData()
             {
             ros_chk_id = 16,
             ros_img = "http://link.to.image/profile.jpg" 
             } });
于 2015-09-15T08:32:30.877 回答
1

尝试这个:

var query = from c in db.clClinics select c; 

var results = query.ToList().Concat(new clClinic[] { new clClinic()});  
于 2011-11-20T19:13:49.610 回答
1

你可以使用联合

IQueryable<int> foo = new SomeQueryable<int>();
IQueryable<int> foo2 = new SomeQueryable<int>();
foo = foo.Union(foo2);
于 2018-11-18T08:40:37.640 回答
0

显式设置匿名类型的属性。尝试这个:

dcDataContext db = new dcDataContext();
var results = from c in db.tblSexes
              select new { c.SexCode, c.SexName };
results = results.AsEnumerable().Concat(new[] { new { SexCode = -1, SexName = "Please select your Gender"} });
SelectList sliSex = new SelectList(results, "SexCode", "SexName");

编辑:您必须指出您的属性的确切类型。我的意思是如果它是 int 你必须指出它是。我猜 SexCode 是一个“long”,默认情况下 -1 是一个“int”。如果您将 -1 转换为 long(或 SexCode 的类型),问题将得到解决。

如果 SexCode 的类型很长,这是确切的解决方案。

dcDataContext db = new dcDataContext();
var results = from c in db.tblSexes
              select new { c.SexCode, c.SexName };
results = results.Concat(new[] { new { SexCode = -1L, SexName = "Please select your Gender"} });
SelectList sliSex = new SelectList(results, "SexCode", "SexName");
于 2009-01-12T09:24:43.347 回答
0

因为结果是 IQueryable 你应该投给它

  dcDataContext db = new dcDataContext();
var results = from c in db.tblSexes
              select new { c.SexCode, c.SexName };
results = results.AsEnumerable().Concat(new[] { new { SexCode = -1, SexName = "Please select your Gender"} }).AsQueryable();
SelectList sliSex = new SelectList(results, "SexCode", "SexName");

或者没有任何铸造

results = results.Union(new[] { new { SexCode = -1, SexName = "请选择您的性别"} })

于 2009-01-12T09:40:23.613 回答