如何在 LINQ to DataSet 中的匿名类中使用基于查询数据的条件?

发布于 2024-08-23 16:28:46 字数 853 浏览 6 评论 0原文

我可以通过循环简化查询的结果来做到这一点:

var query = from myrecord in dt.AsEnumerable()
    where myrecord.Field<string>("field1") == "value1" || myrecord.Field<string>("field1") == "value2"
    select myrecord;

foreach(var myrecord in query)
{
    //if value1, then "X"
    //sum += field2
}

但是,我想知道这是否可以在 LINQ 语句中实现。

具有两个成员的匿名类:名称和值。名称为“X”或“Y”,具体取决于字段 1,值是满足条件的记录的所有字段 2 值的总和。我想我需要使用 Count() 方法,但我不确定如何或在哪里。也许我需要使用“group”和“into”从临时表中获取计数?

如果存在 (field1 == "value1") 的记录,则字符串将为“X”,否则字符串将为“Y”。

var query = from table in dt.AsEnumerable()
    where table.Field<string>("field1") == "value1" || 
        table.Field<string>("field1") == "value2"
    select new
    {
        Name = (condition ? "X" : "Y"),
        Value = //sum all field2 values
    };

提前致谢!

I can do this by looping the result of a simplified query:

var query = from myrecord in dt.AsEnumerable()
    where myrecord.Field<string>("field1") == "value1" || myrecord.Field<string>("field1") == "value2"
    select myrecord;

foreach(var myrecord in query)
{
    //if value1, then "X"
    //sum += field2
}

But, I want to know if it's possible within the LINQ statement.

Anonymous class with two members: Name and Value. Name is "X" or "Y" depending on field1 and Value is the sum of all field2 values for records where the conditions are met. I think I need to use the Count() method, but I'm not sure how or where. Maybe I need to use "group" and "into" to get the count from a temporary table?

If there are records with (field1 == "value1"), the string will be "X" else the string will be "Y".

var query = from table in dt.AsEnumerable()
    where table.Field<string>("field1") == "value1" || 
        table.Field<string>("field1") == "value2"
    select new
    {
        Name = (condition ? "X" : "Y"),
        Value = //sum all field2 values
    };

Thanks in advance!

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

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

发布评论

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

评论(3

万劫不复 2024-08-30 16:28:47

您只需使用已有的“表”变量即可。

var query = from table in dt.AsEnumerable() 
where table.Field<string>("field1") == "value1" ||  
    table.Field<string>("field1") == "value2" 
select new 
{ 
    Name = (table.whatever ? "X" : "Y"), 
    Value = table.something.Sum()
}; 

You just use the "table" variable that you already have.

var query = from table in dt.AsEnumerable() 
where table.Field<string>("field1") == "value1" ||  
    table.Field<string>("field1") == "value2" 
select new 
{ 
    Name = (table.whatever ? "X" : "Y"), 
    Value = table.something.Sum()
}; 
维持三分热 2024-08-30 16:28:47

您可以通过执行以下操作获得表的总和

table.Sum(t => t.ValueToSum) //Or whatever sub-table you need to sum

You can just get the sum of the table by doing

table.Sum(t => t.ValueToSum) //Or whatever sub-table you need to sum
哆啦不做梦 2024-08-30 16:28:46

当然可以,以下示例的工作原理类似

class Program
    {
        static void Main(string[] args)
        {
            List<Person> persons= new List<Person>();
            var result = from p in persons
            select new
             {
              Name = (s.IsMarried ? s.X : s.Y)
             };
        }
    }
    class Person
    {
        public bool IsMarried { get; set; }
        public string X { get; set; }
        public string Y { get; set; }
    }

sure you can, Following sample works similarly

class Program
    {
        static void Main(string[] args)
        {
            List<Person> persons= new List<Person>();
            var result = from p in persons
            select new
             {
              Name = (s.IsMarried ? s.X : s.Y)
             };
        }
    }
    class Person
    {
        public bool IsMarried { get; set; }
        public string X { get; set; }
        public string Y { get; set; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文