[英]loading sequence from except
I have these two tables. 我有这两张桌子。
var cardtagtable = (from u in db.CardTagTables
where u.FKCardTagID == cardtable.cardID
select u.CardTagName).ToList();
var tagtable = (from u in db.TagTables
select u.TagName).ToList();
Where in cardtagtable
all names are selected to match with tagtable
's name. 在
cardtagtable
所有名称都被选择为与tagtable
的名称匹配。
Condition- 条件-
I want names from tagtable
except names coming from cardtagtable
list. 我想要来自
tagtable
名称,但来自cardtagtable
列表的名称除外。
so I tried here- 所以我在这里尝试了
var list = tagtable.Except(cardtagtable);
This list
is a sequence of all names except from cardtagtable
. 该
list
是除cardtagtable
之外的所有名称的序列。
Everything is all right till now. 到现在为止一切都很好。
Now I will use this list
and pass it through the model. 现在,我将使用此
list
并将其传递给模型。
var taglist = (from u in list
select new TagModel {
tagId = u.TagID,
tagName = u.TagName,
tagCount = Convert.ToInt32(u.TagCount) == null ? 0 : Convert.ToInt32(u.TagCount),
}).ToList();
But this query says me no definition found for Model
values from u
. 但是这个查询说我没有找到
u
Model
值的定义。
How do I use this list here in this case? 在这种情况下,如何在此使用此列表?
Because u
is probably a string
because your list is a IEnumerable<string>
.The reason is that you are only selecting your TagNames
, so your list contains only tag names , not your TagTables .Instead you need to select your elements instead of just tag names : 因为
u
可能是一个string
,因为你的列表是一个IEnumerable<string>
。其原因是,你只选择你的TagNames
,所以你的列表只包含标记名称 ,而不是你TagTables。相反,你需要选择你的元素 ,而不仅仅是标签名称 :
var cardtagtable = (from u in db.CardTagTables
where u.FKCardTagID == cardtable.cardID
select u).ToList();
var tagtable = (from u in db.TagTables
select u).ToList();
Then use Where
and Any
instead of Except
like this: 然后使用
Where
and Any
代替Except
这样:
var list = tagtable.Where(c => !cardtagtable
.Any(x => x.CardTagName == c.TagName));
Then your last query should work fine. 然后,您的最后一个查询应该可以正常工作。
Update: Also there is a more elegant and optimized way to do that (especially if this is LINQ to SQL
).You can select only CardtagNames
from cardtagtable
and use Contains
method: 更新:也有一个更优雅和优化的方式做到这一点(特别是如果这是
LINQ to SQL
)。你可以只选择CardtagNames
从cardtagtable
和使用Contains
方法:
var cardtagtable = (from u in db.CardTagTables
where u.FKCardTagID == cardtable.cardID
select u.CardTagName).ToList();
var tagtable = (from u in db.TagTables
select u).ToList();
var list = tagtable.Where(c => !cardtagtable.Contains(c.TagName));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.