LINQ to SQL 省略结果中的字段,同时仍将其包含在 where 子句中
基本上我正在尝试在 LINQ to SQL 中执行此操作;
SELECT DISTINCT a,b,c FROM table WHERE z=35
我已经尝试过这个,(c#代码)
(from record in db.table
select new table {
a = record.a,
b = record.b,
c = record.c
}).Where(record => record.z.Equals(35)).Distinct();
但是当我以这种方式从表对象中删除列 z 时,我得到以下异常;
绑定错误:在投影中找不到成员“table.z”。
我无法返回字段 z,因为它会使我的 unique 变得无用。如有任何帮助,我们将不胜感激,谢谢。
编辑:
这是一个更全面的示例,包括 PredicateBuilder 的使用,
var clause = PredicateBuilder.False<User>();
clause = clause.Or(user => user.z.Equals(35));
foreach (int i in IntegerList) {
int tmp = i;
clause = clause.Or(user => user.a.Equals(tmp));
}
var results = (from u in db.Users
select new User {
a = user.a,
b = user.b,
c = user.c
}).Where(clause).Distinct();
编辑2:
非常感谢大家的评论和回答,这是我最终得到的解决方案,
var clause = PredicateBuilder.False<User>();
clause = clause.Or(user => user.z.Equals(35));
foreach (int i in IntegerList) {
int tmp = i;
clause = clause.Or(user => user.a.Equals(tmp));
}
var results = (from u in db.Users
select u)
.Where(clause)
.Select(u => new User {
a = user.a,
b = user.b,
c = user.c
}).Distinct();
Where 后面跟着 Select 的顺序至关重要。
Basically I'm trying to do this in LINQ to SQL;
SELECT DISTINCT a,b,c FROM table WHERE z=35
I have tried this, (c# code)
(from record in db.table
select new table {
a = record.a,
b = record.b,
c = record.c
}).Where(record => record.z.Equals(35)).Distinct();
But when I remove column z from the table object in that fashion I get the following exception;
Binding error: Member 'table.z' not found in projection.
I can't return field z because it will render my distinct useless. Any help is appreciated, thanks.
Edit:
This is a more comprehensive example that includes the use of PredicateBuilder,
var clause = PredicateBuilder.False<User>();
clause = clause.Or(user => user.z.Equals(35));
foreach (int i in IntegerList) {
int tmp = i;
clause = clause.Or(user => user.a.Equals(tmp));
}
var results = (from u in db.Users
select new User {
a = user.a,
b = user.b,
c = user.c
}).Where(clause).Distinct();
Edit2:
Many thanks to everyone for the comments and answers, this is the solution I ended up with,
var clause = PredicateBuilder.False<User>();
clause = clause.Or(user => user.z.Equals(35));
foreach (int i in IntegerList) {
int tmp = i;
clause = clause.Or(user => user.a.Equals(tmp));
}
var results = (from u in db.Users
select u)
.Where(clause)
.Select(u => new User {
a = user.a,
b = user.b,
c = user.c
}).Distinct();
The ordering of the Where followed by the Select is vital.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
存在问题是因为您的 where 子句位于 linq 查询之外,并且您正在新的匿名数据类型上应用 where 子句,这会导致错误
建议您更改查询,例如
problem is there because you where clause is outside linq query and you are applying the where clause on the new anonymous datatype thats y it causing error
Suggest you to change you query like
不能直接将 WHERE 子句放在 LINQ 中吗?
或者,如果您绝对必须按照编写的方式使用它,请使用 .Select
.Select(r => new { a = ra, b=rb, c=rc }).Distinct();
如此处所示LINQ Select Distinct with Anonymous Types,此方法将起作用,因为它比较匿名类型的所有公共属性。
希望这会有所帮助,不幸的是我对 LINQ 没有太多经验,所以我的回答在专业知识上是有限的。
Can't you just put the WHERE clause in the LINQ?
Alternatively, if you absolutely had to have it the way you wrote it, use .Select
.Select(r => new { a = r.a, b=r.b, c=r.c }).Distinct();
As shown here LINQ Select Distinct with Anonymous Types, this method will work since it compares all public properties of anonymous types.
Hopefully this helps, unfortunately I have not much experience with LINQ so my answer is limited in expertise.