使用 linq to sql 动态选择列(数量)

发布于 2024-11-17 18:09:09 字数 787 浏览 0 评论 0原文

我有一个像这样的表,

    Student   Exam    p1   p2   p3   p4   p5   p6   p7   p8   p9   p10   p11   p12
-----------------------------------------------------------------------------------
    100       unit1   89   56   59   28   48   38   0    0    0     0     0     0
    100       unit2   89   56   59   0     0    0   0    0    0     0     0     0
    100       unit3   89   56   59   28   48   38   0    0    0     0     0     0
    100       unit4   89   56   59   28   48   0    0    0    0     0     0     0

另一个表现

    Exam   Num_subjects
    ----------------------
    unit1     6
    unit2     3
    unit3     6
    unit4     5

在我需要选择单元 1 的标记表中唯一的前 8 列,因为单元 1 的主题数是 6 ..如何动态执行此操作... 考试是 linq to sql 中标记表的外键任何想法...

I have a table like this

    Student   Exam    p1   p2   p3   p4   p5   p6   p7   p8   p9   p10   p11   p12
-----------------------------------------------------------------------------------
    100       unit1   89   56   59   28   48   38   0    0    0     0     0     0
    100       unit2   89   56   59   0     0    0   0    0    0     0     0     0
    100       unit3   89   56   59   28   48   38   0    0    0     0     0     0
    100       unit4   89   56   59   28   48   0    0    0    0     0     0     0

another table

    Exam   Num_subjects
    ----------------------
    unit1     6
    unit2     3
    unit3     6
    unit4     5

now i need to select the only first 8 columns in the marks table for unit1 as the number of subject for the unit1 is 6 .. how to do this dynamically ...
exam is foreign key to the marks table in linq to sql any ideas ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一口甜 2024-11-24 18:09:09

如果您有基于列的设计,由于 L2S 不允许您手动具体化(即 new MyTable { Foo = row.Foo /* omit some } ,您会有点失望。

如果您 只是想要数据,你可以使用像“dapper”这样的东西,它不会有这个问题,但是你需要自己编写TSQL,即

var rows = conn.Query<MyTable>("select p1, p2, p3, p4, p5 from MyTable where Exam=@exam",
        new { exam }).ToList();

但最终,我想我更喜欢不同的数据库模式这里...

If you have a column based design, since L2S doesn't let you manually materialize (i.e. new MyTable { Foo = row.Foo /* omit some } you are a bit scuppered.

If you just want the data, you could use something like "dapper" which won't have this issue, but you'll need to write the TSQL yourself, i.e.

var rows = conn.Query<MyTable>("select p1, p2, p3, p4, p5 from MyTable where Exam=@exam",
        new { exam }).ToList();

But ultimately, I think I'd prefer a different db schema here...

感情洁癖 2024-11-24 18:09:09

如果这些表被规范化,就不需要动态查询。 (检查您的设计)

如果您确实想要动态地执行此操作,则需要一个表达式树来处理查询的选择部分...... asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" rel="nofollow">在这里你可以找到有关 a 的更多详细信息动态查询库可以为您处理表达式树生成(您可以提供像“new(p1,p2,p3)”这样的字符串,并将其转换为表达式树)

there would be no need for a dynamic query, if those tables were normalized. (check your design)

if you really want to do this dynamically, you'll need an expression tree that handles the select part of your query ... here you can find some more details about a dynamic query lib that can handle that expression tree generation for you (you can provide a string like "new(p1,p2,p3)" and that gets translated to an expression tree)

灯角 2024-11-24 18:09:09

这里你不需要使用linq,你可以用逻辑来完成

现在只需获取Num_Subjects就像unit1 = 6

DataTable dt = [whole_table];

int counter = Num_Subjects + 1; //7

string colName = "P" + counter.ToString(); //P7

while(dt.Columns.Contains(colName))
{
   dt.Columns.Remove(colName);
   colName = "P" + (++counter).ToString()
}

最后你我们得到一个表最多P6列其余的列将被删除。

Here you don't need to use linq, you can do it with logic

Now just get Num_Subjects like for unit1 = 6

DataTable dt = [whole_table];

int counter = Num_Subjects + 1; //7

string colName = "P" + counter.ToString(); //P7

while(dt.Columns.Contains(colName))
{
   dt.Columns.Remove(colName);
   colName = "P" + (++counter).ToString()
}

At last you we get a table upto P6 columns rest of columns will be deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文