具有多个条件的数据表上的 LINQ

发布于 2024-11-08 06:54:22 字数 820 浏览 0 评论 0原文

这个查询似乎不太高兴。有人有什么想法吗?

var results = from a in previousQuery
              join b in dtCounties.AsEnumerable()
              on new { a.CountyCode, a.StateCode } equals new { 
                  b.Field<string>("COUNTYCODE"),
                  b.Field<string>("StateCode") 
              }
              where b.Field<bool>("TrueOrFalse") == true
              select new
              {
                  CountyCode = a.CountyCode,
                  TrueOrFalse= b.Field<bool>("TrueOrFalse"),
                  Sum= a.Sum
              };

我得到的错误是

“匿名类型成员声明符无效。必须使用成员赋值、简单名称或成员访问权限来声明匿名类型成员。”

对于连接右侧的 2 列(即 b.Field("COUNTYCODE")b.Field("StateCode"))。

This query just doesnt seem to be very happy. Any ideas anyone?

var results = from a in previousQuery
              join b in dtCounties.AsEnumerable()
              on new { a.CountyCode, a.StateCode } equals new { 
                  b.Field<string>("COUNTYCODE"),
                  b.Field<string>("StateCode") 
              }
              where b.Field<bool>("TrueOrFalse") == true
              select new
              {
                  CountyCode = a.CountyCode,
                  TrueOrFalse= b.Field<bool>("TrueOrFalse"),
                  Sum= a.Sum
              };

The error I get is

"Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access."

for the 2 columns in the right hand side of the join (ie b.Field<string>("COUNTYCODE") and b.Field<string>("StateCode")).

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

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

发布评论

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

评论(2

故事未完 2024-11-15 06:54:22

这只是 C# 语法问题,而不是 LINQ 本身。考虑以下事项:

var a = new { "1" };     // Error
var a = new { a = "1" }; // OK

为了澄清一下,这是上述结果:

on new { cc = a.CountyCode, sc = a.StateCode }
    equals new { cc = b.Field<string>("COUNTYCODE"),
                 sc = b.Field<string>("StateCode") }

This is just a C# syntax issue, not LINQ itself. Consider the following:

var a = new { "1" };     // Error
var a = new { a = "1" }; // OK

Just to clarify, here is the result of the above:

on new { cc = a.CountyCode, sc = a.StateCode }
    equals new { cc = b.Field<string>("COUNTYCODE"),
                 sc = b.Field<string>("StateCode") }
池木 2024-11-15 06:54:22

您需要将字段运算符的结果分配给命名属性。

试试这个:

var results= from a in previousQuery
             join b in dtCounties.AsEnumerable()
             on new { CountryCode = a.CountyCode, StateCode = a.StateCode } 
             equals new 
                    { CountryCode = b.Field<string>("COUNTYCODE") ,
                      StateCode = b.Field<string>("StateCode") }
             where b.Field<bool>("TrueOrFalse") == true
             select new
             {
                    CountyCode = a.CountyCode,
                    TrueOrFalse= b.Field<bool>("TrueOrFalse"),
                    Sum= a.Sum
             };

You need to assign the results of the field operator to named properties.

Try this:

var results= from a in previousQuery
             join b in dtCounties.AsEnumerable()
             on new { CountryCode = a.CountyCode, StateCode = a.StateCode } 
             equals new 
                    { CountryCode = b.Field<string>("COUNTYCODE") ,
                      StateCode = b.Field<string>("StateCode") }
             where b.Field<bool>("TrueOrFalse") == true
             select new
             {
                    CountyCode = a.CountyCode,
                    TrueOrFalse= b.Field<bool>("TrueOrFalse"),
                    Sum= a.Sum
             };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文