LINQ to SQL查询可以使用NOT IN吗?
例如,SELECT au_lname,state FROM authors WHERE state NOT IN('CA','IN','MD')
在各种数据库表中,我有一个属性和一个值列.我正在使用Linq to SQL来访问数据库.
我正在编写一个方法,它返回一个包含从给定数据库表中检索的属性/值的字典:
private static Dictionary<string, string> GetProperties<T>(Table<T> table)
{
Dictionary<string, string> properties = new Dictionary<string, string>();
foreach (var row in table)
{
properties[row.Property]=row.Value;
}
return properties;
}
Run Code Online (Sandbox Code Playgroud)
编译后,我得到:
Error 1 The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Linq.Table<TEntity>'
我试过没有运气的搜索这个错误信息.
搜索StackOverflow时,这个问题似乎相似,但是关于参数List:Generic List <T>作为方法的参数 - 尽管参数仍然不是该问题的答案中的引用类型.
阅读MSDN上的C#编程指南:http://msdn.microsoft.com/en-us/library/twcad0zb(VS.80).aspx我看到他们的例子都通过引用传递参数.但是,在我的特定情况下,我无法看到如何通过引用传递,因为泛型类型仅用于指定Table的泛型类型.
任何指针都将非常感激.
PS:如果我需要时间接受答案,可能会出现这种情况,因为目前无法访问此功能(我是盲人并使用屏幕阅读器).
可我只是点了连接字符串中Dbml.designer.cs在ConnectionString中app.conf?我写下了它成功指向的代码app.config.
public leDataContext() :
base(ConfigurationManager.ConnectionStrings["leConnString"].ToString(), mappingSource)
{
OnCreated();
}
Run Code Online (Sandbox Code Playgroud)
但是每当我修改或向dbml添加一个表时,它都会开始自动将该代码替换为此代码
public leDataContext() :
base("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\My Projects\\App_Data\\le.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True", mappingSource)
{
OnCreated();
}
Run Code Online (Sandbox Code Playgroud)
我扩展了"连接"选项.将"应用程序设置"设置为False
如果我有Linq这样的SQL表达方式:
from subscription in dbContext.Subscriptions
where subscription.Expires > DateTime.Now
select subscription
Run Code Online (Sandbox Code Playgroud)
我希望这使用SQL Server GETDATE()函数而不是运行C#程序的机器的时间.
接下来的问题是如何翻译这个:
DateTime.Now.AddDays(2)
Run Code Online (Sandbox Code Playgroud)
对此:
DATEADD(dd, 2, GETDATE())
Run Code Online (Sandbox Code Playgroud) 我有这样的事情:
var itemsInCart = from o in db.OrderLineItems
where o.OrderId == currentOrder.OrderId
select new { o.OrderLineItemId, ..., ..., o.WishListItem.Price}
Run Code Online (Sandbox Code Playgroud)
有没有办法做
itemsCart.Sum() //not sure what to pass into the function
Run Code Online (Sandbox Code Playgroud)
得到o.WishListItem.Price的总和还是我必须从数据库中获取另一个iQueryable <T> with ... into?
我有几种处理DB的方法,所有这些方法都是从调用开始的
FaierDbDataContext db = new FaierDbDataContext();
Run Code Online (Sandbox Code Playgroud)
由于Linq2Sql DataContext对象实现了IDisposable,它是否应该与"using"一起使用?
using (FaierDbDataContext db = new FaierDbDataContext()) {
// use db here
}
Run Code Online (Sandbox Code Playgroud)
以这种或那种方式使用它有什么含义?
我需要在条件为ORs而不是ANDs的多个条件下进行左连接.我已经找到了很多后者的样本,但我正在努力为我的场景找到正确的答案.
from a in tablea
join b in tableb on new { a.col1, a.col2 } equals new { b.col1, b.col2 }
group a by a into g
select new () { col1 = a.col1, col2 = a.col2, count = g.Count() }
Run Code Online (Sandbox Code Playgroud)
适用于所有条件必须匹配的连接.我需要让联接匹配on a.col1 = b.col1 OR a.col2 = b.col2.
我知道这一定很容易,但我在这个问题上一片空白!
编辑:
为了提供更多信息,查询的目的是获得包含"a"中所有字段的投影以及"b"中匹配记录的计数.我修改了上面的示例,试图说明我追求的是什么.当我使用上述方法运行时,Jon Skeet已经注意到我从a获得了所有记录的计数,而不是b中相关记录的计数.
基本左连接工作正常:
from a in tablea
from b in tableb
.Where( b => ( a.col1 == b.col1 || a.col2 == b.col2))
.DefaultIfEmpty() …Run Code Online (Sandbox Code Playgroud) 如何将其转换为LINQ?
select t.age as AgeRange, count(*) as Users
from (
select case
when age between 0 and 9 then ' 0-25'
when age between 10 and 14 then '26-40'
when age between 20 and 49 then '60-100'
else '50+' end as age
from user) t
group by t.age
Run Code Online (Sandbox Code Playgroud)
谢谢!
是否可以将LinqToSql与PostgreSQL一起使用(最好使用Mono)?你能推荐一步一步描述的文章吗?
所以我有一个字符串列表,如下所示:
var ls=new List<string>()
{
"100",
"101-102-1002",
"105-153-1532-1532",
"105-1854-45-198",
"180-95-45-200"
};
Run Code Online (Sandbox Code Playgroud)
我想得到分裂字符串的倒数第二个.所以我的输出看起来像这样:
null,
102,
1532,
45,
45
Run Code Online (Sandbox Code Playgroud)
我有一个解决方案,看起来像这样:
ls.Select (l =>l.Split('-').Select ((s,i) =>new {s,i})
.OrderByDescending (x=>x.i).Skip(1).Take(1))
Run Code Online (Sandbox Code Playgroud)
我认为这个解决方案对于这个简单的任务可能很复杂.所以我的问题是:你们有没有更简单的解决方案来解决这个问题?
linq-to-sql ×10
c# ×5
linq ×5
sql ×3
.net ×2
.net-3.5 ×1
datacontext ×1
idisposable ×1
mono ×1
postgresql ×1
t-sql ×1